rdwarf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +28 -0
  6. data/README.md +39 -0
  7. data/Rakefile +8 -0
  8. data/bin/console +14 -0
  9. data/bin/setup +7 -0
  10. data/ext/rdwarf/depend +5 -0
  11. data/ext/rdwarf/extconf.rb +29 -0
  12. data/ext/rdwarf/libdwarf/CHANGES +102 -0
  13. data/ext/rdwarf/libdwarf/CODINGSTYLE +71 -0
  14. data/ext/rdwarf/libdwarf/COPYING +28 -0
  15. data/ext/rdwarf/libdwarf/ChangeLog +619 -0
  16. data/ext/rdwarf/libdwarf/ChangeLog2006 +835 -0
  17. data/ext/rdwarf/libdwarf/ChangeLog2007 +217 -0
  18. data/ext/rdwarf/libdwarf/ChangeLog2008 +263 -0
  19. data/ext/rdwarf/libdwarf/ChangeLog2009 +348 -0
  20. data/ext/rdwarf/libdwarf/ChangeLog2010 +175 -0
  21. data/ext/rdwarf/libdwarf/ChangeLog2011 +297 -0
  22. data/ext/rdwarf/libdwarf/ChangeLog2012 +131 -0
  23. data/ext/rdwarf/libdwarf/ChangeLog2013 +238 -0
  24. data/ext/rdwarf/libdwarf/ChangeLog2014 +399 -0
  25. data/ext/rdwarf/libdwarf/LGPL.txt +504 -0
  26. data/ext/rdwarf/libdwarf/LIBDWARFCOPYRIGHT +40 -0
  27. data/ext/rdwarf/libdwarf/Makefile.in +220 -0
  28. data/ext/rdwarf/libdwarf/NEWS +535 -0
  29. data/ext/rdwarf/libdwarf/README +235 -0
  30. data/ext/rdwarf/libdwarf/checkexamples.c +1179 -0
  31. data/ext/rdwarf/libdwarf/cmplrs/dwarf_addr_finder.h +55 -0
  32. data/ext/rdwarf/libdwarf/common.c +62 -0
  33. data/ext/rdwarf/libdwarf/common.h +38 -0
  34. data/ext/rdwarf/libdwarf/config.h.in +146 -0
  35. data/ext/rdwarf/libdwarf/configure +5581 -0
  36. data/ext/rdwarf/libdwarf/configure.in +167 -0
  37. data/ext/rdwarf/libdwarf/dw-linetableheader.txt +39 -0
  38. data/ext/rdwarf/libdwarf/dwarf.h +1342 -0
  39. data/ext/rdwarf/libdwarf/dwarf_abbrev.c +291 -0
  40. data/ext/rdwarf/libdwarf/dwarf_abbrev.h +45 -0
  41. data/ext/rdwarf/libdwarf/dwarf_addr_finder.c +676 -0
  42. data/ext/rdwarf/libdwarf/dwarf_alloc.c +685 -0
  43. data/ext/rdwarf/libdwarf/dwarf_alloc.h +38 -0
  44. data/ext/rdwarf/libdwarf/dwarf_arange.c +595 -0
  45. data/ext/rdwarf/libdwarf/dwarf_arange.h +62 -0
  46. data/ext/rdwarf/libdwarf/dwarf_base_types.h +157 -0
  47. data/ext/rdwarf/libdwarf/dwarf_die_deliv.c +1802 -0
  48. data/ext/rdwarf/libdwarf/dwarf_die_deliv.h +46 -0
  49. data/ext/rdwarf/libdwarf/dwarf_elf_access.c +1348 -0
  50. data/ext/rdwarf/libdwarf/dwarf_elf_access.h +46 -0
  51. data/ext/rdwarf/libdwarf/dwarf_error.c +492 -0
  52. data/ext/rdwarf/libdwarf/dwarf_error.h +53 -0
  53. data/ext/rdwarf/libdwarf/dwarf_form.c +1302 -0
  54. data/ext/rdwarf/libdwarf/dwarf_frame.c +2454 -0
  55. data/ext/rdwarf/libdwarf/dwarf_frame.h +418 -0
  56. data/ext/rdwarf/libdwarf/dwarf_frame2.c +1533 -0
  57. data/ext/rdwarf/libdwarf/dwarf_frame3.c +282 -0
  58. data/ext/rdwarf/libdwarf/dwarf_funcs.c +123 -0
  59. data/ext/rdwarf/libdwarf/dwarf_funcs.h +33 -0
  60. data/ext/rdwarf/libdwarf/dwarf_gdbindex.c +520 -0
  61. data/ext/rdwarf/libdwarf/dwarf_gdbindex.h +97 -0
  62. data/ext/rdwarf/libdwarf/dwarf_global.c +612 -0
  63. data/ext/rdwarf/libdwarf/dwarf_global.h +117 -0
  64. data/ext/rdwarf/libdwarf/dwarf_harmless.c +228 -0
  65. data/ext/rdwarf/libdwarf/dwarf_harmless.h +31 -0
  66. data/ext/rdwarf/libdwarf/dwarf_incl.h +61 -0
  67. data/ext/rdwarf/libdwarf/dwarf_init_finish.c +1263 -0
  68. data/ext/rdwarf/libdwarf/dwarf_leb.c +159 -0
  69. data/ext/rdwarf/libdwarf/dwarf_line.c +1822 -0
  70. data/ext/rdwarf/libdwarf/dwarf_line.h +446 -0
  71. data/ext/rdwarf/libdwarf/dwarf_line2.c +98 -0
  72. data/ext/rdwarf/libdwarf/dwarf_line_table_reader_common.c +1583 -0
  73. data/ext/rdwarf/libdwarf/dwarf_loc.c +1525 -0
  74. data/ext/rdwarf/libdwarf/dwarf_loc.h +149 -0
  75. data/ext/rdwarf/libdwarf/dwarf_loc2.c +833 -0
  76. data/ext/rdwarf/libdwarf/dwarf_macro.c +479 -0
  77. data/ext/rdwarf/libdwarf/dwarf_macro.h +35 -0
  78. data/ext/rdwarf/libdwarf/dwarf_opaque.h +778 -0
  79. data/ext/rdwarf/libdwarf/dwarf_original_elf_init.c +219 -0
  80. data/ext/rdwarf/libdwarf/dwarf_print_lines.c +631 -0
  81. data/ext/rdwarf/libdwarf/dwarf_pubtypes.c +132 -0
  82. data/ext/rdwarf/libdwarf/dwarf_query.c +1594 -0
  83. data/ext/rdwarf/libdwarf/dwarf_ranges.c +194 -0
  84. data/ext/rdwarf/libdwarf/dwarf_reloc_arm.h +308 -0
  85. data/ext/rdwarf/libdwarf/dwarf_reloc_mips.h +117 -0
  86. data/ext/rdwarf/libdwarf/dwarf_reloc_ppc.h +242 -0
  87. data/ext/rdwarf/libdwarf/dwarf_reloc_ppc64.h +272 -0
  88. data/ext/rdwarf/libdwarf/dwarf_reloc_x86_64.h +127 -0
  89. data/ext/rdwarf/libdwarf/dwarf_sort_line.c +665 -0
  90. data/ext/rdwarf/libdwarf/dwarf_string.c +82 -0
  91. data/ext/rdwarf/libdwarf/dwarf_stubs.c +38 -0
  92. data/ext/rdwarf/libdwarf/dwarf_tied.c +423 -0
  93. data/ext/rdwarf/libdwarf/dwarf_tsearch.h +125 -0
  94. data/ext/rdwarf/libdwarf/dwarf_tsearchhash.c +675 -0
  95. data/ext/rdwarf/libdwarf/dwarf_types.c +121 -0
  96. data/ext/rdwarf/libdwarf/dwarf_types.h +32 -0
  97. data/ext/rdwarf/libdwarf/dwarf_util.c +913 -0
  98. data/ext/rdwarf/libdwarf/dwarf_util.h +324 -0
  99. data/ext/rdwarf/libdwarf/dwarf_vars.c +125 -0
  100. data/ext/rdwarf/libdwarf/dwarf_vars.h +29 -0
  101. data/ext/rdwarf/libdwarf/dwarf_weaks.c +123 -0
  102. data/ext/rdwarf/libdwarf/dwarf_weaks.h +29 -0
  103. data/ext/rdwarf/libdwarf/dwarf_xu_index.c +579 -0
  104. data/ext/rdwarf/libdwarf/dwarf_xu_index.h +68 -0
  105. data/ext/rdwarf/libdwarf/dwgetopt.c +181 -0
  106. data/ext/rdwarf/libdwarf/dwgetopt.h +51 -0
  107. data/ext/rdwarf/libdwarf/gennames.c +531 -0
  108. data/ext/rdwarf/libdwarf/install.sh +119 -0
  109. data/ext/rdwarf/libdwarf/libdwarf.h.in +3746 -0
  110. data/ext/rdwarf/libdwarf/libdwarf2.1.mm +9805 -0
  111. data/ext/rdwarf/libdwarf/libdwarf2.1.pdf +0 -0
  112. data/ext/rdwarf/libdwarf/libdwarf2p.1.mm +2807 -0
  113. data/ext/rdwarf/libdwarf/libdwarf2p.1.pdf +0 -0
  114. data/ext/rdwarf/libdwarf/libdwarfdefs.h +81 -0
  115. data/ext/rdwarf/libdwarf/malloc_check.c +327 -0
  116. data/ext/rdwarf/libdwarf/malloc_check.h +52 -0
  117. data/ext/rdwarf/libdwarf/mips_extensions.mm +1266 -0
  118. data/ext/rdwarf/libdwarf/mips_extensions.pdf +0 -0
  119. data/ext/rdwarf/libdwarf/pro_alloc.c +179 -0
  120. data/ext/rdwarf/libdwarf/pro_alloc.h +33 -0
  121. data/ext/rdwarf/libdwarf/pro_arange.c +310 -0
  122. data/ext/rdwarf/libdwarf/pro_arange.h +51 -0
  123. data/ext/rdwarf/libdwarf/pro_die.c +431 -0
  124. data/ext/rdwarf/libdwarf/pro_die.h +59 -0
  125. data/ext/rdwarf/libdwarf/pro_encode_nm.c +108 -0
  126. data/ext/rdwarf/libdwarf/pro_encode_nm.h +39 -0
  127. data/ext/rdwarf/libdwarf/pro_error.c +96 -0
  128. data/ext/rdwarf/libdwarf/pro_error.h +43 -0
  129. data/ext/rdwarf/libdwarf/pro_expr.c +575 -0
  130. data/ext/rdwarf/libdwarf/pro_expr.h +36 -0
  131. data/ext/rdwarf/libdwarf/pro_finish.c +45 -0
  132. data/ext/rdwarf/libdwarf/pro_forms.c +1271 -0
  133. data/ext/rdwarf/libdwarf/pro_frame.c +572 -0
  134. data/ext/rdwarf/libdwarf/pro_frame.h +120 -0
  135. data/ext/rdwarf/libdwarf/pro_funcs.c +50 -0
  136. data/ext/rdwarf/libdwarf/pro_incl.h +91 -0
  137. data/ext/rdwarf/libdwarf/pro_init.c +327 -0
  138. data/ext/rdwarf/libdwarf/pro_line.c +373 -0
  139. data/ext/rdwarf/libdwarf/pro_line.h +112 -0
  140. data/ext/rdwarf/libdwarf/pro_macinfo.c +457 -0
  141. data/ext/rdwarf/libdwarf/pro_macinfo.h +31 -0
  142. data/ext/rdwarf/libdwarf/pro_opaque.h +513 -0
  143. data/ext/rdwarf/libdwarf/pro_pubnames.c +60 -0
  144. data/ext/rdwarf/libdwarf/pro_reloc.c +253 -0
  145. data/ext/rdwarf/libdwarf/pro_reloc.h +38 -0
  146. data/ext/rdwarf/libdwarf/pro_reloc_stream.c +256 -0
  147. data/ext/rdwarf/libdwarf/pro_reloc_stream.h +52 -0
  148. data/ext/rdwarf/libdwarf/pro_reloc_symbolic.c +245 -0
  149. data/ext/rdwarf/libdwarf/pro_reloc_symbolic.h +45 -0
  150. data/ext/rdwarf/libdwarf/pro_section.c +2233 -0
  151. data/ext/rdwarf/libdwarf/pro_section.h +100 -0
  152. data/ext/rdwarf/libdwarf/pro_types.c +274 -0
  153. data/ext/rdwarf/libdwarf/pro_types.h +34 -0
  154. data/ext/rdwarf/libdwarf/pro_util.h +38 -0
  155. data/ext/rdwarf/libdwarf/pro_vars.c +52 -0
  156. data/ext/rdwarf/libdwarf/pro_weaks.c +51 -0
  157. data/ext/rdwarf/rdwarf.c +765 -0
  158. data/ext/rdwarf/rdwarf.h +52 -0
  159. data/ext/rdwarf/rdwarf_names_gen.rb +109 -0
  160. data/lib/rdwarf.rb +181 -0
  161. data/lib/rdwarf/version.rb +3 -0
  162. data/rdwarf.gemspec +30 -0
  163. metadata +251 -0
@@ -0,0 +1,46 @@
1
+ /*
2
+
3
+ Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4
+ Portions Copyright (C) 2008-2011 David Anderson. All Rights Reserved.
5
+
6
+ This program is free software; you can redistribute it and/or modify it
7
+ under the terms of version 2.1 of the GNU Lesser General Public License
8
+ as published by the Free Software Foundation.
9
+
10
+ This program is distributed in the hope that it would be useful, but
11
+ WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
+
14
+ Further, this software is distributed without any warranty that it is
15
+ free of the rightful claim of any third person regarding infringement
16
+ or the like. Any license provided herein, whether implied or
17
+ otherwise, applies only to this software file. Patent licenses, if
18
+ any, provided herein do not apply to combinations of this program with
19
+ other software, or any other product whatsoever.
20
+
21
+ You should have received a copy of the GNU Lesser General Public
22
+ License along with this program; if not, write the Free Software
23
+ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
24
+ USA.
25
+
26
+ */
27
+
28
+
29
+
30
+
31
+ /*
32
+ This struct holds information about a abbreviation.
33
+ It is put in the hash table for abbreviations for
34
+ a compile-unit.
35
+ */
36
+ struct Dwarf_Abbrev_List_s {
37
+ Dwarf_Unsigned ab_code;
38
+ Dwarf_Half ab_tag;
39
+ Dwarf_Half ab_has_child;
40
+
41
+ /* Points to start of attribute and form pairs in the .debug_abbrev
42
+ section for the abbrev. */
43
+ Dwarf_Byte_Ptr ab_abbrev_ptr;
44
+
45
+ struct Dwarf_Abbrev_List_s *ab_next;
46
+ };
@@ -0,0 +1,1348 @@
1
+ /*
2
+ Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
3
+ Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
4
+ Portions Copyright 2008-2010 Arxan Technologies, Inc. All Rights Reserved.
5
+ Portions Copyright 2009-2012 David Anderson. All rights reserved.
6
+ Portions Copyright 2009-2010 Novell Inc. All rights reserved.
7
+ Portions Copyright 2012 SN Systems Ltd. All rights reserved.
8
+
9
+ This program is free software; you can redistribute it and/or modify it
10
+ under the terms of version 2.1 of the GNU Lesser General Public License
11
+ as published by the Free Software Foundation.
12
+
13
+ This program is distributed in the hope that it would be useful, but
14
+ WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
+
17
+ Further, this software is distributed without any warranty that it is
18
+ free of the rightful claim of any third person regarding infringement
19
+ or the like. Any license provided herein, whether implied or
20
+ otherwise, applies only to this software file. Patent licenses, if
21
+ any, provided herein do not apply to combinations of this program with
22
+ other software, or any other product whatsoever.
23
+
24
+ You should have received a copy of the GNU Lesser General Public
25
+ License along with this program; if not, write the Free Software
26
+ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
27
+ USA.
28
+
29
+ */
30
+
31
+ #include "config.h"
32
+ #include "dwarf_incl.h"
33
+ #include "dwarf_elf_access.h"
34
+
35
+ /* Include Relocation definitions in the case of Windows */
36
+ #ifdef WIN32
37
+ #include "dwarf_reloc_arm.h"
38
+ #include "dwarf_reloc_mips.h"
39
+ #include "dwarf_reloc_ppc.h"
40
+ #include "dwarf_reloc_ppc64.h"
41
+ #include "dwarf_reloc_x86_64.h"
42
+ #endif /* WIN32 */
43
+
44
+ #ifdef HAVE_ELF_H
45
+ #include <elf.h>
46
+ #endif
47
+ #ifdef HAVE_LIBELF_H
48
+ #include <libelf.h>
49
+ #else
50
+ #ifdef HAVE_LIBELF_LIBELF_H
51
+ #include <libelf/libelf.h>
52
+ #endif
53
+ #endif
54
+
55
+ #include <stdio.h>
56
+ #include <sys/stat.h>
57
+ #include <sys/types.h>
58
+ #include <string.h>
59
+ #include <stdlib.h>
60
+
61
+ #define FALSE 0
62
+ #define TRUE 1
63
+
64
+ #ifndef EM_MIPS
65
+ /* This is the standard elf value EM_MIPS. */
66
+ #define EM_MIPS 8
67
+ #endif
68
+
69
+ #ifndef EM_K10M
70
+ #define EM_K10M 181 /* Intel K10M */
71
+ #endif
72
+ #ifndef EM_L10M
73
+ #define EM_L10M 180 /* Intel L10M */
74
+ #endif
75
+ #ifndef EM_AARCH64
76
+ #define EM_AARCH64 183 /* Arm 64 */
77
+ #endif
78
+ #ifndef R_AARCH64_ABS64
79
+ #define R_AARCH64_ABS64 0x101
80
+ #endif
81
+ #ifndef R_AARCH64_ABS32
82
+ #define R_AARCH64_ABS32 0x102
83
+ #endif
84
+ #ifndef R_MIPS_64
85
+ #define R_MIPS_64 18
86
+ #endif
87
+ #ifndef R_MIPS_TLS_TPREL64
88
+ #define R_MIPS_TLS_TPREL64 48
89
+ #endif
90
+
91
+ #ifndef EM_IA_64
92
+ #define EM_IA_64 50
93
+ #endif
94
+ #ifndef R_IA64_SECREL32LSB
95
+ #define R_IA64_SECREL32LSB 0x65
96
+ #endif
97
+ #ifndef R_IA64_DIR32MSB
98
+ #define R_IA64_DIR32MSB 0x24
99
+ #endif
100
+ #ifndef R_IA64_DIR32LSB
101
+ #define R_IA64_DIR32LSB 0x25
102
+ #endif
103
+ #ifndef R_IA64_DIR64MSB
104
+ #define R_IA64_DIR64MSB 0x26
105
+ #endif
106
+ #ifndef R_IA64_DIR64LSB
107
+ #define R_IA64_DIR64LSB 0x27
108
+ #endif
109
+ #ifndef R_IA64_SECREL64LSB
110
+ #define R_IA64_SECREL64LSB 0x67
111
+ #endif
112
+ #ifndef R_IA64_SECREL64MSB
113
+ #define R_IA64_SECREL64MSB 0x66
114
+ #endif
115
+ #ifndef R_IA64_DTPREL32LSB
116
+ #define R_IA64_DTPREL32LSB 0xb5
117
+ #endif
118
+ #ifndef R_IA64_DTPREL32MSB
119
+ #define R_IA64_DTPREL32MSB 0xb4
120
+ #endif
121
+ #ifndef R_IA64_DTPREL64LSB
122
+ #define R_IA64_DTPREL64LSB 0xb7
123
+ #endif
124
+ #ifndef R_IA64_DTPREL64MSB
125
+ #define R_IA64_DTPREL64MSB 0xb6
126
+ #endif
127
+
128
+ #ifndef EM_S390
129
+ #define EM_S390 22
130
+ #endif
131
+ #ifndef R_390_TLS_LDO32
132
+ #define R_390_TLS_LDO32 52
133
+ #endif
134
+ #ifndef R_390_TLS_LDO64
135
+ #define R_390_TLS_LDO64 53
136
+ #endif
137
+
138
+ #ifndef R_390_32
139
+ #define R_390_32 4
140
+ #endif
141
+ #ifndef R_390_64
142
+ #define R_390_64 22
143
+ #endif
144
+
145
+ #ifndef EM_SH
146
+ #define EM_SH 42
147
+ #endif
148
+ #ifndef R_SH_DIR32
149
+ #define R_SH_DIR32 1
150
+ #endif
151
+ #ifndef R_SH_TLS_DTPOFF32
152
+ #define R_SH_TLS_DTPOFF32 150
153
+ #endif
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+ #ifdef HAVE_ELF64_GETEHDR
162
+ extern Elf64_Ehdr *elf64_getehdr(Elf *);
163
+ #endif
164
+ #ifdef HAVE_ELF64_GETSHDR
165
+ extern Elf64_Shdr *elf64_getshdr(Elf_Scn *);
166
+ #endif
167
+ #ifdef WORDS_BIGENDIAN
168
+ #define WRITE_UNALIGNED(dbg,dest,source, srclength,len_out) \
169
+ { \
170
+ dbg->de_copy_word(dest, \
171
+ ((char *)source) +srclength-len_out, \
172
+ len_out) ; \
173
+ }
174
+
175
+
176
+ #else /* LITTLE ENDIAN */
177
+
178
+ #define WRITE_UNALIGNED(dbg,dest,source, srclength,len_out) \
179
+ { \
180
+ dbg->de_copy_word( (dest) , \
181
+ ((char *)source) , \
182
+ len_out) ; \
183
+ }
184
+ #endif
185
+
186
+
187
+
188
+ typedef struct {
189
+ dwarf_elf_handle elf;
190
+ int is_64bit;
191
+ Dwarf_Small length_size;
192
+ Dwarf_Small pointer_size;
193
+ Dwarf_Unsigned section_count;
194
+ Dwarf_Endianness endianness;
195
+ Dwarf_Small machine;
196
+ int libdwarf_owns_elf;
197
+ Elf32_Ehdr *ehdr32;
198
+
199
+ #ifdef HAVE_ELF64_GETEHDR
200
+ Elf64_Ehdr *ehdr64;
201
+ #endif
202
+ /* Elf symtab and its strtab. Initialized at first
203
+ call to do relocations, the actual data is in the Dwarf_Debug
204
+ struct, not allocated locally here. */
205
+ struct Dwarf_Section_s *symtab;
206
+ struct Dwarf_Section_s *strtab;
207
+
208
+ } dwarf_elf_object_access_internals_t;
209
+
210
+ struct Dwarf_Elf_Rela {
211
+ Dwarf_ufixed64 r_offset;
212
+ /*Dwarf_ufixed64 r_info; */
213
+ Dwarf_ufixed64 r_type;
214
+ Dwarf_ufixed64 r_symidx;
215
+ Dwarf_ufixed64 r_addend;
216
+ };
217
+
218
+
219
+ static int dwarf_elf_object_access_load_section(void* obj_in,
220
+ Dwarf_Half section_index,
221
+ Dwarf_Small** section_data,
222
+ int* error);
223
+
224
+ /* dwarf_elf_object_access_internals_init()
225
+ On error, set *error with libdwarf error code.
226
+ */
227
+ static int
228
+ dwarf_elf_object_access_internals_init(void* obj_in,
229
+ dwarf_elf_handle elf,
230
+ int* error)
231
+ {
232
+ dwarf_elf_object_access_internals_t*obj =
233
+ (dwarf_elf_object_access_internals_t*)obj_in;
234
+ char *ehdr_ident = 0;
235
+ Dwarf_Half machine = 0;
236
+ obj->elf = elf;
237
+
238
+ if ((ehdr_ident = elf_getident(elf, NULL)) == NULL) {
239
+ *error = DW_DLE_ELF_GETIDENT_ERROR;
240
+ return DW_DLV_ERROR;
241
+ }
242
+
243
+ obj->is_64bit = (ehdr_ident[EI_CLASS] == ELFCLASS64);
244
+
245
+
246
+ if (ehdr_ident[EI_DATA] == ELFDATA2LSB){
247
+ obj->endianness = DW_OBJECT_LSB;
248
+ } else if (ehdr_ident[EI_DATA] == ELFDATA2MSB){
249
+ obj->endianness = DW_OBJECT_MSB;
250
+ }
251
+
252
+ if (obj->is_64bit) {
253
+ #ifdef HAVE_ELF64_GETEHDR
254
+ obj->ehdr64 = elf64_getehdr(elf);
255
+ if (obj->ehdr64 == NULL) {
256
+ *error = DW_DLE_ELF_GETEHDR_ERROR;
257
+ return DW_DLV_ERROR;
258
+ }
259
+ obj->section_count = obj->ehdr64->e_shnum;
260
+ machine = obj->ehdr64->e_machine;
261
+ obj->machine = machine;
262
+ #else
263
+ *error = DW_DLE_NO_ELF64_SUPPORT;
264
+ return DW_DLV_ERROR;
265
+ #endif
266
+ } else {
267
+ obj->ehdr32 = elf32_getehdr(elf);
268
+ if (obj->ehdr32 == NULL) {
269
+ *error = DW_DLE_ELF_GETEHDR_ERROR;
270
+ return DW_DLV_ERROR;
271
+ }
272
+ obj->section_count = obj->ehdr32->e_shnum;
273
+ machine = obj->ehdr32->e_machine;
274
+ obj->machine = machine;
275
+ }
276
+
277
+ /* The following length_size is Not Too Significant. Only used
278
+ one calculation, and an approximate one at that. */
279
+ obj->length_size = obj->is_64bit ? 8 : 4;
280
+ obj->pointer_size = obj->is_64bit ? 8 : 4;
281
+
282
+ #ifdef WIN32
283
+ if (obj->is_64bit && machine == EM_PPC64) {
284
+ /* The SNC compiler generates the EM_PPC64 machine type for the
285
+ PS3 platform, but is a 32 bits pointer size in user mode. */
286
+ obj->pointer_size = 4;
287
+ }
288
+ #endif /* WIN32 */
289
+
290
+ if (obj->is_64bit && machine != EM_MIPS) {
291
+ /* MIPS/IRIX makes pointer size and length size 8 for -64.
292
+ Other platforms make length 4 always. */
293
+ /* 4 here supports 32bit-offset dwarf2, as emitted by cygnus
294
+ tools, and the dwarfv2.1 64bit extension setting.
295
+ This is not the same as the size-of-an-offset, which
296
+ is 4 in 32bit dwarf and 8 in 64bit dwarf. */
297
+ obj->length_size = 4;
298
+ }
299
+ return DW_DLV_OK;
300
+ }
301
+
302
+ /* dwarf_elf_object_access_get_byte_order */
303
+ static
304
+ Dwarf_Endianness
305
+ dwarf_elf_object_access_get_byte_order(void* obj_in)
306
+ {
307
+ dwarf_elf_object_access_internals_t*obj =
308
+ (dwarf_elf_object_access_internals_t*)obj_in;
309
+ return obj->endianness;
310
+ }
311
+
312
+ /* dwarf_elf_object_access_get_section_count() */
313
+ static
314
+ Dwarf_Unsigned
315
+ dwarf_elf_object_access_get_section_count(void * obj_in)
316
+ {
317
+ dwarf_elf_object_access_internals_t*obj =
318
+ (dwarf_elf_object_access_internals_t*)obj_in;
319
+ return obj->section_count;
320
+ }
321
+
322
+
323
+ /* dwarf_elf_object_access_get_section()
324
+
325
+ If writing a function vaguely like this for a non-elf object,
326
+ be sure that when section-index is passed in as zero that
327
+ you set the fields in *ret_scn to reflect an empty section
328
+ with an empty string as the section name. Adjust your
329
+ section indexes of your non-elf-reading-code
330
+ for all the necessary functions in Dwarf_Obj_Access_Methods_s
331
+ accordingly.
332
+ */
333
+ static
334
+ int
335
+ dwarf_elf_object_access_get_section_info(
336
+ void* obj_in,
337
+ Dwarf_Half section_index,
338
+ Dwarf_Obj_Access_Section* ret_scn,
339
+ int* error)
340
+ {
341
+ dwarf_elf_object_access_internals_t*obj =
342
+ (dwarf_elf_object_access_internals_t*)obj_in;
343
+
344
+ Elf32_Shdr *shdr32 = 0;
345
+
346
+ #ifdef HAVE_ELF64_GETSHDR
347
+ Elf64_Shdr *shdr64 = 0;
348
+ #endif
349
+ Elf_Scn *scn = 0;
350
+
351
+
352
+ scn = elf_getscn(obj->elf, section_index);
353
+ if (scn == NULL) {
354
+ *error = DW_DLE_MDE;
355
+ return DW_DLV_ERROR;
356
+ }
357
+ if (obj->is_64bit) {
358
+ #ifdef HAVE_ELF64_GETSHDR
359
+ shdr64 = elf64_getshdr(scn);
360
+ if (shdr64 == NULL) {
361
+ *error = DW_DLE_ELF_GETSHDR_ERROR;
362
+ return DW_DLV_ERROR;
363
+ }
364
+
365
+ /* Get also section 'sh_type' and sh_info' fields, so the caller
366
+ can use it for additional tasks that require that info. */
367
+ ret_scn->type = shdr64->sh_type;
368
+ ret_scn->size = shdr64->sh_size;
369
+ ret_scn->addr = shdr64->sh_addr;
370
+ ret_scn->link = shdr64->sh_link;
371
+ ret_scn->info = shdr64->sh_info;
372
+ ret_scn->entrysize = shdr64->sh_entsize;
373
+ ret_scn->name = elf_strptr(obj->elf, obj->ehdr64->e_shstrndx,
374
+ shdr64->sh_name);
375
+ if (ret_scn->name == NULL) {
376
+ *error = DW_DLE_ELF_STRPTR_ERROR;
377
+ return DW_DLV_ERROR;
378
+ }
379
+ return DW_DLV_OK;
380
+ #else
381
+ *error = DW_DLE_MISSING_ELF64_SUPPORT;
382
+ return DW_DLV_ERROR;
383
+ #endif /* HAVE_ELF64_GETSHDR */
384
+ }
385
+ if ((shdr32 = elf32_getshdr(scn)) == NULL) {
386
+ *error = DW_DLE_ELF_GETSHDR_ERROR;
387
+ return DW_DLV_ERROR;
388
+ }
389
+
390
+ /* Get also the section type, so the caller can use it for
391
+ additional tasks that require to know the section type. */
392
+ ret_scn->type = shdr32->sh_type;
393
+ ret_scn->size = shdr32->sh_size;
394
+ ret_scn->addr = shdr32->sh_addr;
395
+ ret_scn->link = shdr32->sh_link;
396
+ ret_scn->info = shdr32->sh_info;
397
+ ret_scn->entrysize = shdr32->sh_entsize;
398
+ ret_scn->name = elf_strptr(obj->elf, obj->ehdr32->e_shstrndx,
399
+ shdr32->sh_name);
400
+ if (ret_scn->name == NULL) {
401
+ *error = DW_DLE_ELF_STRPTR_ERROR;
402
+ return DW_DLV_ERROR;
403
+ }
404
+ return DW_DLV_OK;
405
+ }
406
+
407
+ /* dwarf_elf_object_access_get_length_size */
408
+ static
409
+ Dwarf_Small
410
+ dwarf_elf_object_access_get_length_size(void* obj_in)
411
+ {
412
+ dwarf_elf_object_access_internals_t*obj =
413
+ (dwarf_elf_object_access_internals_t*)obj_in;
414
+ return obj->length_size;
415
+ }
416
+
417
+ /* dwarf_elf_object_access_get_pointer_size */
418
+ static
419
+ Dwarf_Small
420
+ dwarf_elf_object_access_get_pointer_size(void* obj_in)
421
+ {
422
+ dwarf_elf_object_access_internals_t*obj =
423
+ (dwarf_elf_object_access_internals_t*)obj_in;
424
+ return obj->pointer_size;
425
+ }
426
+
427
+ #define MATCH_REL_SEC(i_,s_,r_) \
428
+ if (i_ == s_.dss_index) { \
429
+ *r_ = &s_; \
430
+ return DW_DLV_OK; \
431
+ }
432
+
433
+ static int
434
+ find_section_to_relocate(Dwarf_Debug dbg,Dwarf_Half section_index,
435
+ struct Dwarf_Section_s **relocatablesec, int *error)
436
+ {
437
+ MATCH_REL_SEC(section_index,dbg->de_debug_info,relocatablesec);
438
+ MATCH_REL_SEC(section_index,dbg->de_debug_abbrev,relocatablesec);
439
+ MATCH_REL_SEC(section_index,dbg->de_debug_line,relocatablesec);
440
+ MATCH_REL_SEC(section_index,dbg->de_debug_loc,relocatablesec);
441
+ MATCH_REL_SEC(section_index,dbg->de_debug_aranges,relocatablesec);
442
+ MATCH_REL_SEC(section_index,dbg->de_debug_macinfo,relocatablesec);
443
+ MATCH_REL_SEC(section_index,dbg->de_debug_pubnames,relocatablesec);
444
+ MATCH_REL_SEC(section_index,dbg->de_debug_ranges,relocatablesec);
445
+ MATCH_REL_SEC(section_index,dbg->de_debug_frame,relocatablesec);
446
+ MATCH_REL_SEC(section_index,dbg->de_debug_frame_eh_gnu,relocatablesec);
447
+ MATCH_REL_SEC(section_index,dbg->de_debug_pubtypes,relocatablesec);
448
+ MATCH_REL_SEC(section_index,dbg->de_debug_funcnames,relocatablesec);
449
+ MATCH_REL_SEC(section_index,dbg->de_debug_typenames,relocatablesec);
450
+ MATCH_REL_SEC(section_index,dbg->de_debug_varnames,relocatablesec);
451
+ MATCH_REL_SEC(section_index,dbg->de_debug_weaknames,relocatablesec);
452
+ MATCH_REL_SEC(section_index,dbg->de_debug_types,relocatablesec);
453
+ /* dbg-> de_debug_tu_index,reloctablesec); */
454
+ /* dbg-> de_debug_cu_index,reloctablesec); */
455
+ /* dbg-> de_debug_gdbindex,reloctablesec); */
456
+ /* dbg-> de_debug_str,syms); */
457
+ /* de_elf_symtab,syms); */
458
+ /* de_elf_strtab,syms); */
459
+ *error = DW_DLE_RELOC_SECTION_MISMATCH;
460
+ return DW_DLV_ERROR;
461
+
462
+ }
463
+ #undef MATCH_REL_SEC
464
+
465
+ static void
466
+ get_rela_elf32(Dwarf_Small *data, unsigned int i,
467
+ int endianness,
468
+ int machine,
469
+ struct Dwarf_Elf_Rela *relap)
470
+ {
471
+ Elf32_Rela *relp = (Elf32_Rela*)(data + (i * sizeof(Elf32_Rela)));
472
+ relap->r_offset = relp->r_offset;
473
+ /*
474
+ relap->r_info = relp->r_info;
475
+ */
476
+ relap->r_type = ELF32_R_TYPE(relp->r_info);
477
+ relap->r_symidx = ELF32_R_SYM(relp->r_info);
478
+ relap->r_addend = relp->r_addend;
479
+ }
480
+
481
+ static void
482
+ get_rela_elf64(Dwarf_Small *data, unsigned int i,
483
+ int endianness,
484
+ int machine,
485
+ struct Dwarf_Elf_Rela *relap)
486
+ {
487
+ #ifdef HAVE_ELF64_RELA
488
+ Elf64_Rela * relp = (Elf64_Rela*)(data + (i * sizeof(Elf64_Rela)));
489
+ relap->r_offset = relp->r_offset;
490
+ /*
491
+ relap->r_info = relp->r_info;
492
+ */
493
+ #define ELF64MIPS_REL_SYM(i) ((i) & 0xffffffff)
494
+ #define ELF64MIPS_REL_TYPE(i) ((i >> 56) &0xff)
495
+ if (machine == EM_MIPS && endianness == DW_OBJECT_LSB ){
496
+ /* This is really wierd. Treat this very specially.
497
+ The Elf64 LE MIPS object used for
498
+ testing (that has rela) wants the
499
+ values as sym ssym type3 type2 type, treating
500
+ each value as independent value. But libelf xlate
501
+ treats it as something else so we fudge here.
502
+ It is unclear
503
+ how to precisely characterize where these relocations
504
+ were used.
505
+ SGI MIPS on IRIX never used .rela relocations.
506
+ The BE 64bit elf MIPS test object with rela uses traditional
507
+ elf relocation layouts, not this special case. */
508
+ /* We ignore the special TYPE2 and TYPE3, they should be
509
+ value R_MIPS_NONE in rela. */
510
+ relap->r_type = ELF64MIPS_REL_TYPE(relp->r_info);
511
+ relap->r_symidx = ELF64MIPS_REL_SYM(relp->r_info);
512
+ #undef MIPS64SYM
513
+ #undef MIPS64TYPE
514
+ } else
515
+ {
516
+ relap->r_type = ELF64_R_TYPE(relp->r_info);
517
+ relap->r_symidx = ELF64_R_SYM(relp->r_info);
518
+ }
519
+ relap->r_addend = relp->r_addend;
520
+ #endif
521
+ }
522
+
523
+ static void
524
+ get_relocations_array(Dwarf_Bool is_64bit,
525
+ int endianness,
526
+ int machine,
527
+ Dwarf_Small *data,
528
+ unsigned int num_relocations,
529
+ struct Dwarf_Elf_Rela *relap)
530
+ {
531
+ unsigned int i = 0;
532
+ void (*get_relocations)(Dwarf_Small *data, unsigned int i,
533
+ int endianness,
534
+ int machine,
535
+ struct Dwarf_Elf_Rela *relap);
536
+
537
+ /* Handle 32/64 bit issue */
538
+ if (is_64bit) {
539
+ get_relocations = get_rela_elf64;
540
+ } else {
541
+ get_relocations = get_rela_elf32;
542
+ }
543
+
544
+ for (i=0; i < num_relocations; i++) {
545
+ get_relocations(data, i,endianness,machine,
546
+ &(relap[i]));
547
+ }
548
+
549
+ }
550
+
551
+ static int
552
+ get_relocation_entries(Dwarf_Bool is_64bit,
553
+ int endianness,
554
+ int machine,
555
+ Dwarf_Small *relocation_section,
556
+ Dwarf_Unsigned relocation_section_size,
557
+ Dwarf_Unsigned relocation_section_entrysize,
558
+ struct Dwarf_Elf_Rela **relas,
559
+ unsigned int *nrelas,
560
+ int *error)
561
+ {
562
+ unsigned int relocation_size = 0;
563
+
564
+ if (is_64bit) {
565
+ #ifdef HAVE_ELF64_RELA
566
+ relocation_size = sizeof(Elf64_Rela);
567
+ #else
568
+ *error = DW_DLE_MISSING_ELF64_SUPPORT;
569
+ return DW_DLV_ERROR;
570
+ #endif
571
+ } else {
572
+ relocation_size = sizeof(Elf32_Rela);
573
+ }
574
+ if (relocation_size != relocation_section_entrysize) {
575
+ /* Means our struct definition does not match the
576
+ real object. */
577
+ *error = DW_DLE_RELOC_SECTION_LENGTH_ODD;
578
+ return DW_DLV_ERROR;
579
+ }
580
+
581
+ if (relocation_section == NULL) {
582
+ *error = DW_DLE_RELOC_SECTION_PTR_NULL;
583
+ return(DW_DLV_ERROR);
584
+ }
585
+
586
+ if ((relocation_section_size != 0)) {
587
+ size_t bytescount = 0;
588
+ if (relocation_section_size%relocation_size) {
589
+ *error = DW_DLE_RELOC_SECTION_LENGTH_ODD;
590
+ return DW_DLV_ERROR;
591
+ }
592
+ *nrelas = relocation_section_size/relocation_size;
593
+ bytescount = (*nrelas) * sizeof(struct Dwarf_Elf_Rela);
594
+ *relas = malloc(bytescount);
595
+ if (!*relas) {
596
+ *error = DW_DLE_MAF;
597
+ return(DW_DLV_ERROR);
598
+ }
599
+ memset(*relas,0,bytescount);
600
+ get_relocations_array(is_64bit,endianness,machine,
601
+ relocation_section,
602
+ *nrelas, *relas);
603
+ }
604
+ return(DW_DLV_OK);
605
+ }
606
+
607
+ /* We have a EM_QUALCOMM_DSP6 relocatable object
608
+ test case in dwarf regression tests, atefail/ig_server.
609
+ Values for QUALCOMM were derived from this executable.
610
+
611
+ The r = 0 in the function will get optimized away
612
+ when not needed.
613
+
614
+ */
615
+
616
+ #define EM_QUALCOMM_DSP6 0xa4
617
+ #define QUALCOMM_REL32 6
618
+
619
+ static Dwarf_Bool
620
+ is_32bit_abs_reloc(unsigned int type, Dwarf_Half machine)
621
+ {
622
+ Dwarf_Bool r = 0;
623
+ switch (machine) {
624
+ #if defined(EM_MIPS) && defined (R_MIPS_32)
625
+ case EM_MIPS:
626
+ r = (0
627
+ #if defined (R_MIPS_32)
628
+ | (type == R_MIPS_32)
629
+ #endif
630
+ #if defined (R_MIPS_TLS_DTPREL32)
631
+ | (type == R_MIPS_TLS_DTPREL32)
632
+ #endif /* DTPREL32 */
633
+ );
634
+ break;
635
+ #endif /* MIPS case */
636
+ #if defined(EM_SPARC32PLUS) && defined (R_SPARC_UA32)
637
+ case EM_SPARC32PLUS:
638
+ r = (type == R_SPARC_UA32);
639
+ break;
640
+ #endif
641
+ #if defined(EM_SPARCV9) && defined (R_SPARC_UA32)
642
+ case EM_SPARCV9:
643
+ r = (type == R_SPARC_UA32);
644
+ break;
645
+ #endif
646
+ #if defined(EM_SPARC) && defined (R_SPARC_UA32)
647
+ case EM_SPARC:
648
+ r = (0
649
+ #if defined(R_SPARC_UA32)
650
+ | (type == R_SPARC_UA32)
651
+ #endif
652
+ #if (R_SPARC_TLS_DTPOFF32)
653
+ | (type == R_SPARC_TLS_DTPOFF32)
654
+ #endif
655
+ );
656
+ break;
657
+ #endif /* EM_SPARC */
658
+ #if defined(EM_386) && defined (R_386_32)
659
+ case EM_386:
660
+ r = (0
661
+ #if defined (R_386_32)
662
+ | (type == R_386_32)
663
+ #endif
664
+ #if defined (R_386_TLS_LDO_32)
665
+ | (type == R_386_TLS_LDO_32)
666
+ #endif
667
+ #if defined (R_386_TLS_DTPOFF32)
668
+ | (type == R_386_TLS_DTPOFF32)
669
+ #endif
670
+ );
671
+ break;
672
+ #endif /* EM_386 */
673
+
674
+ #if defined (EM_SH) && defined (R_SH_DIR32)
675
+ case EM_SH:
676
+ r = (0
677
+ #if defined (R_SH_DIR32)
678
+ | (type == R_SH_DIR32)
679
+ #endif
680
+ #if defined (R_SH_DTPOFF32)
681
+ | (type == R_SH_TLS_DTPOFF32)
682
+ #endif
683
+ );
684
+ break;
685
+ #endif /* SH */
686
+
687
+ #if defined(EM_IA_64) && defined (R_IA64_SECREL32LSB)
688
+ case EM_IA_64: /* 32bit? ! */
689
+ r = (0
690
+ #if defined (R_IA64_SECREL32LSB)
691
+ | (type == R_IA64_SECREL32LSB)
692
+ #endif
693
+ #if defined (R_IA64_DIR32LSB)
694
+ | (type == R_IA64_DIR32LSB)
695
+ #endif
696
+ #if defined (R_IA64_DTPREL32LSB)
697
+ | (type == R_IA64_DTPREL32LSB)
698
+ #endif
699
+ );
700
+ break;
701
+ #endif /* EM_IA_64 */
702
+
703
+ #if defined(EM_ARM) && defined (R_ARM_ABS32)
704
+ case EM_ARM:
705
+ case EM_AARCH64:
706
+ r = (0
707
+ #if defined (R_ARM_ABS32)
708
+ | ( type == R_ARM_ABS32)
709
+ #endif
710
+ #if defined (R_AARCH64_ABS32)
711
+ | ( type == R_AARCH64_ABS32)
712
+ #endif
713
+ #if defined (R_ARM_TLS_LDO32)
714
+ | ( type == R_ARM_TLS_LDO32)
715
+ #endif
716
+ );
717
+ break;
718
+ #endif /* EM_ARM */
719
+
720
+ /* On FreeBSD R_PPC64_ADDR32 not defined
721
+ so we use the R_PPC_ names which
722
+ have the proper value.
723
+ Our headers have:
724
+ R_PPC64_ADDR64 38
725
+ R_PPC_ADDR32 1 so we use this one
726
+ R_PPC64_ADDR32 R_PPC_ADDR32
727
+
728
+ R_PPC64_DTPREL32 110 which may be wrong/unavailable
729
+ R_PPC64_DTPREL64 78
730
+ R_PPC_DTPREL32 78
731
+ */
732
+ #if defined(EM_PPC64) && defined (R_PPC_ADDR32)
733
+ case EM_PPC64:
734
+ r = (0
735
+ #if defined(R_PPC_ADDR32)
736
+ | (type == R_PPC_ADDR32)
737
+ #endif
738
+ #if defined(R_PPC64_DTPREL32)
739
+ | (type == R_PPC64_DTPREL32)
740
+ #endif
741
+ );
742
+ break;
743
+ #endif /* EM_PPC64 */
744
+
745
+
746
+ #if defined(EM_PPC) && defined (R_PPC_ADDR32)
747
+ case EM_PPC:
748
+ r = (0
749
+ #if defined (R_PPC_ADDR32)
750
+ | (type == R_PPC_ADDR32)
751
+ #endif
752
+ #if defined (R_PPC_DTPREL32)
753
+ | (type == R_PPC_DTPREL32)
754
+ #endif
755
+ );
756
+ break;
757
+ #endif /* EM_PPC */
758
+
759
+ #if defined(EM_S390) && defined (R_390_32)
760
+ case EM_S390:
761
+ r = (0
762
+ #if defined (R_390_32)
763
+ | (type == R_390_32)
764
+ #endif
765
+ #if defined (R_390_TLS_LDO32)
766
+ | (type == R_390_TLS_LDO32)
767
+ #endif
768
+ );
769
+ break;
770
+ #endif /* EM_S390 */
771
+
772
+ #if defined(EM_X86_64) && defined (R_X86_64_32)
773
+ #if defined(EM_K10M)
774
+ case EM_K10M:
775
+ #endif
776
+ #if defined(EM_L10M)
777
+ case EM_L10M:
778
+ #endif
779
+ case EM_X86_64:
780
+ r = (0
781
+ #if defined (R_X86_64_32)
782
+ | (type == R_X86_64_32)
783
+ #endif
784
+ #if defined (R_X86_64_DTPOFF32)
785
+ | (type == R_X86_64_DTPOFF32)
786
+ #endif
787
+ );
788
+ break;
789
+ #endif /* EM_X86_64 */
790
+
791
+ case EM_QUALCOMM_DSP6:
792
+ r = (type == QUALCOMM_REL32);
793
+ break;
794
+ }
795
+ return r;
796
+ }
797
+
798
+ static Dwarf_Bool
799
+ is_64bit_abs_reloc(unsigned int type, Dwarf_Half machine)
800
+ {
801
+ Dwarf_Bool r = 0;
802
+ switch (machine) {
803
+ #if defined(EM_MIPS) && defined (R_MIPS_64)
804
+ case EM_MIPS:
805
+ r = (0
806
+ #if defined (R_MIPS_64)
807
+ | (type == R_MIPS_64)
808
+ #endif
809
+ #if defined (R_MIPS_32)
810
+ | (type == R_MIPS_32)
811
+ #endif
812
+ #if defined(R_MIPS_TLS_DTPREL64)
813
+ | (type == R_MIPS_TLS_DTPREL64)
814
+ #endif
815
+ );
816
+ break;
817
+ #endif /* EM_MIPS */
818
+ #if defined(EM_SPARC32PLUS) && defined (R_SPARC_UA64)
819
+ case EM_SPARC32PLUS:
820
+ r = (type == R_SPARC_UA64);
821
+ break;
822
+ #endif
823
+ #if defined(EM_SPARCV9) && defined (R_SPARC_UA64)
824
+ case EM_SPARCV9:
825
+ r = (0
826
+ #if defined (R_SPARC_UA64)
827
+ | (type == R_SPARC_UA64)
828
+ #endif
829
+ #if defined (R_SPARC_TLS_DTPOFF64)
830
+ | (type == R_SPARC_TLS_DTPOFF64)
831
+ #endif
832
+ );
833
+ break;
834
+ #endif
835
+ #if defined(EM_SPARC) && defined (R_SPARC_UA64)
836
+ case EM_SPARC:
837
+ r = (0
838
+ #if defined(R_SPARC_UA64)
839
+ | (type == R_SPARC_UA64)
840
+ #endif
841
+ #if defined (R_SPARC_TLS_DTPOFF64)
842
+ | (type == R_SPARC_TLS_DTPOFF64)
843
+ #endif
844
+ );
845
+ break;
846
+ #endif /* EM_SPARC */
847
+
848
+ #if defined(EM_IA_64) && defined (R_IA64_SECREL64LSB)
849
+ case EM_IA_64: /* 64bit */
850
+ r = (0
851
+ #if defined (R_IA64_SECREL64LSB)
852
+ | (type == R_IA64_SECREL64LSB)
853
+ #endif
854
+ #if defined (R_IA64_SECREL32LSB)
855
+ | (type == R_IA64_SECREL32LSB)
856
+ #endif
857
+ #if defined (R_IA64_DIR64LSB)
858
+ | (type == R_IA64_DIR64LSB)
859
+ #endif
860
+ #if defined (R_IA64_DTPREL64LSB)
861
+ | (type == R_IA64_DTPREL64LSB)
862
+ #endif
863
+ #if defined (R_IA64_REL32LSB)
864
+ | (type == R_IA64_REL32LSB)
865
+ #endif
866
+ );
867
+ break;
868
+ #endif /* EM_IA_64 */
869
+
870
+ #if defined(EM_PPC64) && defined (R_PPC64_ADDR64)
871
+ case EM_PPC64:
872
+ r = (0
873
+ #if defined(R_PPC64_ADDR64)
874
+ | (type == R_PPC64_ADDR64)
875
+ #endif
876
+ #if defined(R_PPC64_DTPREL64)
877
+ | (type == R_PPC64_DTPREL64)
878
+ #endif
879
+ );
880
+ break;
881
+ #endif /* EM_PPC64 */
882
+
883
+ #if defined(EM_S390) && defined (R_390_64)
884
+ case EM_S390:
885
+ r = (0
886
+ #if defined(R_390_64)
887
+ | (type == R_390_64)
888
+ #endif
889
+ #if defined(R_390_TLS_LDO64)
890
+ | (type == R_390_TLS_LDO64)
891
+ #endif
892
+ );
893
+ break;
894
+ #endif /* EM_390 */
895
+
896
+ #if defined(EM_X86_64) && defined (R_X86_64_64)
897
+ #if defined(EM_K10M)
898
+ case EM_K10M:
899
+ #endif
900
+ #if defined(EM_L10M)
901
+ case EM_L10M:
902
+ #endif
903
+ case EM_X86_64:
904
+ r = (0
905
+ #if defined (R_X86_64_64)
906
+ | (type == R_X86_64_64)
907
+ #endif
908
+ #if defined (R_X86_64_DTPOFF32)
909
+ | (type == R_X86_64_DTPOFF64)
910
+ #endif
911
+ );
912
+ break;
913
+ #endif /* EM_X86_64 */
914
+ #if defined(EM_AARCH64) && defined (R_AARCH64_ABS64)
915
+ case EM_AARCH64:
916
+ r = (0
917
+ #if defined (R_AARCH64_ABS64)
918
+ | ( type == R_AARCH64_ABS64)
919
+ #endif
920
+ );
921
+ break;
922
+ #endif /* EM_AARCH64 */
923
+
924
+ }
925
+ return r;
926
+ }
927
+
928
+
929
+ /* Returns DW_DLV_OK if it works, else DW_DLV_ERROR.
930
+ The caller may decide to ignore the errors or report them. */
931
+ static int
932
+ update_entry(Dwarf_Debug dbg,
933
+ Dwarf_Bool is_64bit, Dwarf_Endianness endianess,
934
+ Dwarf_Half machine, struct Dwarf_Elf_Rela *rela,
935
+ Dwarf_Small *target_section,
936
+ Dwarf_Small *symtab_section_data,
937
+ Dwarf_Unsigned symtab_section_size,
938
+ Dwarf_Unsigned symtab_section_entrysize,
939
+ int *error)
940
+ {
941
+ unsigned int type = 0;
942
+ unsigned int sym_idx = 0;
943
+ #ifdef HAVE_ELF64_SYM
944
+ Elf64_Sym sym_buf;
945
+ Elf64_Sym *sym = 0;
946
+ #else
947
+ Elf32_Sym sym_buf;
948
+ Elf32_Sym *sym = 0;
949
+ #endif
950
+ Elf32_Sym *sym32 = 0;
951
+ Dwarf_ufixed64 offset = 0;
952
+ Dwarf_sfixed64 addend = 0;
953
+ Dwarf_Unsigned reloc_size = 0;
954
+ Dwarf_Unsigned symtab_entry_count = 0;
955
+
956
+ if (symtab_section_entrysize == 0) {
957
+ *error = DW_DLE_SYMTAB_SECTION_ENTRYSIZE_ZERO;
958
+ return DW_DLV_ERROR;
959
+ }
960
+ symtab_entry_count = symtab_section_size/symtab_section_entrysize;
961
+
962
+ /* Dwarf_Elf_Rela dereferencing */
963
+ offset = rela->r_offset;
964
+ addend = rela->r_addend;
965
+ type = rela->r_type;
966
+ sym_idx = rela->r_symidx;
967
+ if (sym_idx >= symtab_entry_count) {
968
+ *error = DW_DLE_RELOC_SECTION_SYMBOL_INDEX_BAD;
969
+ return DW_DLV_ERROR;
970
+ }
971
+
972
+
973
+
974
+ if (is_64bit) {
975
+ #ifdef HAVE_ELF64_SYM
976
+ sym = &((Elf64_Sym*)symtab_section_data)[sym_idx];
977
+ #endif
978
+ } else {
979
+ sym32 = &((Elf32_Sym*)symtab_section_data)[sym_idx];
980
+
981
+ /* Convert Elf32_Sym struct to Elf64_Sym struct. We point at
982
+ an Elf64_Sym local variable (sym_buf) to allow us to use the
983
+ same pointer (sym) for both 32-bit and 64-bit instances. */
984
+ sym = &sym_buf;
985
+ sym->st_name = sym32->st_name;
986
+ sym->st_info = sym32->st_info;
987
+ sym->st_other = sym32->st_other;
988
+ sym->st_shndx = sym32->st_shndx;
989
+ sym->st_value = sym32->st_value;
990
+ sym->st_size = sym32->st_size;
991
+ }
992
+
993
+ /* Determine relocation size */
994
+ if (is_32bit_abs_reloc(type, machine)) {
995
+ reloc_size = 4;
996
+ } else if (is_64bit_abs_reloc(type, machine)) {
997
+ reloc_size = 8;
998
+ } else {
999
+ *error = DW_DLE_RELOC_SECTION_RELOC_TARGET_SIZE_UNKNOWN;
1000
+ return DW_DLV_ERROR;
1001
+ }
1002
+
1003
+
1004
+ {
1005
+ /* Assuming we do not need to do a READ_UNALIGNED here
1006
+ at target_section + offset and add its value to
1007
+ outval. Some ABIs say no read (for example MIPS),
1008
+ but if some do then which ones? */
1009
+ Dwarf_Unsigned outval = sym->st_value + addend;
1010
+ WRITE_UNALIGNED(dbg,target_section + offset,
1011
+ &outval,sizeof(outval),reloc_size);
1012
+ }
1013
+ return DW_DLV_OK;
1014
+ }
1015
+
1016
+
1017
+
1018
+ /* Somewhat arbitrarily, we attempt to apply all the relocations we can
1019
+ and still notify the caller of at least one error if we found
1020
+ any errors. */
1021
+ static int
1022
+ apply_rela_entries(Dwarf_Debug dbg,
1023
+ Dwarf_Bool is_64bit,
1024
+ Dwarf_Endianness endianess,
1025
+ Dwarf_Half machine,
1026
+ Dwarf_Small *target_section,
1027
+ Dwarf_Small *symtab_section,
1028
+ Dwarf_Unsigned symtab_section_size,
1029
+ Dwarf_Unsigned symtab_section_entrysize,
1030
+ struct Dwarf_Elf_Rela *relas, unsigned int nrelas,
1031
+ int *error)
1032
+ {
1033
+ int return_res = DW_DLV_OK;
1034
+ if ((target_section != NULL) && (relas != NULL)) {
1035
+ unsigned int i;
1036
+ if (symtab_section_entrysize == 0) {
1037
+ *error = DW_DLE_SYMTAB_SECTION_ENTRYSIZE_ZERO;
1038
+ return DW_DLV_ERROR;
1039
+ }
1040
+ if (symtab_section_size%symtab_section_entrysize) {
1041
+ *error = DW_DLE_SYMTAB_SECTION_LENGTH_ODD;
1042
+ return DW_DLV_ERROR;
1043
+ }
1044
+ for (i = 0; i < nrelas; i++) {
1045
+ int res = update_entry(dbg, is_64bit,
1046
+ endianess,
1047
+ machine,
1048
+ &(relas)[i],
1049
+ target_section,
1050
+ symtab_section,
1051
+ symtab_section_size,
1052
+ symtab_section_entrysize,
1053
+ error);
1054
+ if (res != DW_DLV_OK) {
1055
+ return_res = res;
1056
+ }
1057
+ }
1058
+ }
1059
+ return return_res;
1060
+ }
1061
+
1062
+
1063
+ static int
1064
+ loop_through_relocations(
1065
+ Dwarf_Debug dbg,
1066
+ dwarf_elf_object_access_internals_t* obj,
1067
+ struct Dwarf_Section_s *relocatablesec,
1068
+ int *error)
1069
+ {
1070
+ Dwarf_Small *target_section = 0;
1071
+ Dwarf_Small *symtab_section = obj->symtab->dss_data;
1072
+ Dwarf_Unsigned symtab_section_entrysize = obj->symtab->dss_entrysize;
1073
+ Dwarf_Unsigned symtab_section_size = obj->symtab->dss_size;
1074
+ Dwarf_Small *relocation_section = relocatablesec->dss_reloc_data;
1075
+ Dwarf_Unsigned relocation_section_size =
1076
+ relocatablesec->dss_reloc_size;
1077
+ Dwarf_Unsigned relocation_section_entrysize = relocatablesec->dss_reloc_entrysize;
1078
+
1079
+ int ret = DW_DLV_ERROR;
1080
+ struct Dwarf_Elf_Rela *relas = 0;
1081
+ unsigned int nrelas = 0;
1082
+ Dwarf_Small *mspace = 0;
1083
+
1084
+ ret = get_relocation_entries(obj->is_64bit,
1085
+ obj->endianness,
1086
+ obj->machine,
1087
+ relocation_section,
1088
+ relocation_section_size,
1089
+ relocation_section_entrysize,
1090
+ &relas, &nrelas, error);
1091
+ if (ret != DW_DLV_OK) {
1092
+ free(relas);
1093
+ return ret;
1094
+ }
1095
+
1096
+ /* Some systems read Elf in read-only memory via mmap or the like.
1097
+ So the only safe thing is to copy the current data into
1098
+ malloc space and refer to the malloc space instead of the
1099
+ space returned by the elf library */
1100
+ mspace = malloc(relocatablesec->dss_size);
1101
+ if (!mspace) {
1102
+ *error = DW_DLE_RELOC_SECTION_MALLOC_FAIL;
1103
+ return DW_DLV_ERROR;
1104
+ }
1105
+ memcpy(mspace,relocatablesec->dss_data,relocatablesec->dss_size);
1106
+ relocatablesec->dss_data = mspace;
1107
+ target_section = relocatablesec->dss_data;
1108
+ relocatablesec->dss_data_was_malloc = 1;
1109
+
1110
+ ret = apply_rela_entries(
1111
+ dbg,
1112
+ obj->is_64bit,
1113
+ obj->endianness, obj->machine,
1114
+ target_section,
1115
+ symtab_section,
1116
+ symtab_section_size,
1117
+ symtab_section_entrysize,
1118
+ relas, nrelas, error);
1119
+ free(relas);
1120
+ return ret;
1121
+ }
1122
+
1123
+ /* Find the section data in dbg and find all the relevant
1124
+ sections. Then do relocations.
1125
+ */
1126
+ static int
1127
+ dwarf_elf_object_relocate_a_section(void* obj_in,
1128
+ Dwarf_Half section_index,
1129
+ Dwarf_Debug dbg,
1130
+ int* error)
1131
+ {
1132
+ int res = DW_DLV_ERROR;
1133
+ dwarf_elf_object_access_internals_t*obj = 0;
1134
+ struct Dwarf_Section_s * relocatablesec = 0;
1135
+ if (section_index == 0) {
1136
+ return DW_DLV_NO_ENTRY;
1137
+ }
1138
+ obj = (dwarf_elf_object_access_internals_t*)obj_in;
1139
+
1140
+ /* The section to relocate must already be loaded into memory. */
1141
+ res = find_section_to_relocate(dbg, section_index,&relocatablesec,error);
1142
+ if (res != DW_DLV_OK) {
1143
+ return res;
1144
+ }
1145
+
1146
+ /* Sun and possibly others do not always set sh_link in .debug_* sections.
1147
+ So we cannot do full consistency checks. */
1148
+ if (relocatablesec->dss_reloc_index == 0 ) {
1149
+ /* Something is wrong. */
1150
+ *error = DW_DLE_RELOC_SECTION_MISSING_INDEX;
1151
+ return DW_DLV_ERROR;
1152
+ }
1153
+ /* Now load the relocations themselves. */
1154
+ res = dwarf_elf_object_access_load_section(obj_in,
1155
+ relocatablesec->dss_reloc_index,
1156
+ &relocatablesec->dss_reloc_data, error);
1157
+ if (res != DW_DLV_OK) {
1158
+ return res;
1159
+ }
1160
+
1161
+ /* Now get the symtab. */
1162
+ if (!obj->symtab) {
1163
+ obj->symtab = &dbg->de_elf_symtab;
1164
+ obj->strtab = &dbg->de_elf_strtab;
1165
+ }
1166
+ if (obj->symtab->dss_index != relocatablesec->dss_reloc_link) {
1167
+ /* Something is wrong. */
1168
+ *error = DW_DLE_RELOC_MISMATCH_RELOC_INDEX;
1169
+ return DW_DLV_ERROR;
1170
+ }
1171
+ if (obj->strtab->dss_index != obj->symtab->dss_link) {
1172
+ /* Something is wrong. */
1173
+ *error = DW_DLE_RELOC_MISMATCH_STRTAB_INDEX;
1174
+ return DW_DLV_ERROR;
1175
+ }
1176
+ if (!obj->symtab->dss_data) {
1177
+ /* Now load the symtab */
1178
+ res = dwarf_elf_object_access_load_section(obj_in,
1179
+ obj->symtab->dss_index,
1180
+ &obj->symtab->dss_data, error);
1181
+ if (res != DW_DLV_OK) {
1182
+ return res;
1183
+ }
1184
+ }
1185
+ if (!obj->strtab->dss_data) {
1186
+ /* Now load the strtab */
1187
+ res = dwarf_elf_object_access_load_section(obj_in,
1188
+ obj->strtab->dss_index,
1189
+ &obj->strtab->dss_data,error);
1190
+ if (res != DW_DLV_OK){
1191
+ return res;
1192
+ }
1193
+ }
1194
+
1195
+ /* We have all the data we need in memory. */
1196
+ res = loop_through_relocations(dbg,obj,relocatablesec,error);
1197
+
1198
+ return res;
1199
+ }
1200
+
1201
+ /* dwarf_elf_object_access_load_section */
1202
+ static int
1203
+ dwarf_elf_object_access_load_section(void* obj_in,
1204
+ Dwarf_Half section_index,
1205
+ Dwarf_Small** section_data,
1206
+ int* error)
1207
+ {
1208
+ dwarf_elf_object_access_internals_t*obj =
1209
+ (dwarf_elf_object_access_internals_t*)obj_in;
1210
+ if (section_index == 0) {
1211
+ return DW_DLV_NO_ENTRY;
1212
+ }
1213
+
1214
+ {
1215
+ Elf_Scn *scn = 0;
1216
+ Elf_Data *data = 0;
1217
+
1218
+ scn = elf_getscn(obj->elf, section_index);
1219
+ if (scn == NULL) {
1220
+ *error = DW_DLE_MDE;
1221
+ return DW_DLV_ERROR;
1222
+ }
1223
+
1224
+ /* When using libelf as a producer, section data may be stored
1225
+ in multiple buffers. In libdwarf however, we only use libelf
1226
+ as a consumer (there is a dwarf producer API, but it doesn't
1227
+ use libelf). Because of this, this single call to elf_getdata
1228
+ will retrieve the entire section in a single contiguous
1229
+ buffer. */
1230
+ data = elf_getdata(scn, NULL);
1231
+ if (data == NULL) {
1232
+ *error = DW_DLE_MDE;
1233
+ return DW_DLV_ERROR;
1234
+ }
1235
+ *section_data = data->d_buf;
1236
+ }
1237
+ return DW_DLV_OK;
1238
+ }
1239
+
1240
+
1241
+ /* dwarf_elf_access method table. */
1242
+ static const struct Dwarf_Obj_Access_Methods_s dwarf_elf_object_access_methods =
1243
+ {
1244
+ dwarf_elf_object_access_get_section_info,
1245
+ dwarf_elf_object_access_get_byte_order,
1246
+ dwarf_elf_object_access_get_length_size,
1247
+ dwarf_elf_object_access_get_pointer_size,
1248
+ dwarf_elf_object_access_get_section_count,
1249
+ dwarf_elf_object_access_load_section,
1250
+ dwarf_elf_object_relocate_a_section
1251
+ };
1252
+
1253
+
1254
+ /* Interface for the ELF object file implementation.
1255
+ On error this should set *err with the
1256
+ libdwarf error code.
1257
+ */
1258
+ int
1259
+ dwarf_elf_object_access_init(dwarf_elf_handle elf,
1260
+ int libdwarf_owns_elf,
1261
+ Dwarf_Obj_Access_Interface** ret_obj,
1262
+ int *err)
1263
+ {
1264
+ int res = 0;
1265
+ dwarf_elf_object_access_internals_t *internals = 0;
1266
+ Dwarf_Obj_Access_Interface *intfc = 0;
1267
+
1268
+ internals = malloc(sizeof(dwarf_elf_object_access_internals_t));
1269
+ if (!internals) {
1270
+ *err = DW_DLE_ALLOC_FAIL;
1271
+ /* Impossible case, we hope. Give up. */
1272
+ return DW_DLV_ERROR;
1273
+ }
1274
+ memset(internals,0,sizeof(*internals));
1275
+ res = dwarf_elf_object_access_internals_init(internals, elf, err);
1276
+ if (res != DW_DLV_OK){
1277
+ /* *err is already set. */
1278
+ free(internals);
1279
+ return DW_DLV_ERROR;
1280
+ }
1281
+ internals->libdwarf_owns_elf = libdwarf_owns_elf;
1282
+
1283
+ intfc = malloc(sizeof(Dwarf_Obj_Access_Interface));
1284
+ if (!intfc) {
1285
+ /* Impossible case, we hope. Give up. */
1286
+ *err = DW_DLE_ALLOC_FAIL;
1287
+ free(internals);
1288
+ return DW_DLV_ERROR;
1289
+ }
1290
+ /* Initialize the interface struct */
1291
+ intfc->object = internals;
1292
+ intfc->methods = &dwarf_elf_object_access_methods;
1293
+
1294
+ *ret_obj = intfc;
1295
+ return DW_DLV_OK;
1296
+ }
1297
+
1298
+
1299
+
1300
+ /* Clean up the Dwarf_Obj_Access_Interface returned by elf_access_init. */
1301
+ void
1302
+ dwarf_elf_object_access_finish(Dwarf_Obj_Access_Interface* obj)
1303
+ {
1304
+ if (!obj) {
1305
+ return;
1306
+ }
1307
+ if (obj->object) {
1308
+ dwarf_elf_object_access_internals_t *internals =
1309
+ (dwarf_elf_object_access_internals_t *)obj->object;
1310
+ if (internals->libdwarf_owns_elf){
1311
+ elf_end(internals->elf);
1312
+ }
1313
+ }
1314
+ free(obj->object);
1315
+ free(obj);
1316
+ }
1317
+
1318
+ /* This function returns the Elf * pointer
1319
+ associated with a Dwarf_Debug.
1320
+
1321
+ This function only makes sense if ELF is implied. */
1322
+ int
1323
+ dwarf_get_elf(Dwarf_Debug dbg, dwarf_elf_handle * elf,
1324
+ Dwarf_Error * error)
1325
+ {
1326
+ struct Dwarf_Obj_Access_Interface_s * obj = 0;
1327
+ if (dbg == NULL) {
1328
+ _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
1329
+ return (DW_DLV_ERROR);
1330
+ }
1331
+
1332
+ obj = dbg->de_obj_file;
1333
+ if (obj) {
1334
+ dwarf_elf_object_access_internals_t *internals =
1335
+ (dwarf_elf_object_access_internals_t*)obj->object;
1336
+ if (internals->elf == NULL) {
1337
+ _dwarf_error(dbg, error, DW_DLE_FNO);
1338
+ return (DW_DLV_ERROR);
1339
+ }
1340
+ *elf = internals->elf;
1341
+ return DW_DLV_OK;
1342
+
1343
+ }
1344
+ _dwarf_error(dbg, error, DW_DLE_FNO);
1345
+ return DW_DLV_ERROR;
1346
+ }
1347
+
1348
+