Debian Codegraph @

src/lib/libdwarfp/dwarf_pro_init.c

raw
1 /*
2 Copyright (C) 2000,2004 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2002-2010 Sun Microsystems, Inc. All rights reserved.
4 Portions Copyright 2008-2017 David Anderson, Inc. All rights reserved.
5 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
6
7 This program is free software; you can redistribute it
8 and/or modify it under the terms of version 2.1 of the
9 GNU Lesser General Public License as published by the Free
10 Software Foundation.
11
12 This program is distributed in the hope that it would be
13 useful, but WITHOUT ANY WARRANTY; without even the implied
14 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE.
16
17 Further, this software is distributed without any warranty
18 that it is free of the rightful claim of any third person
19 regarding infringement or the like. Any license provided
20 herein, whether implied or otherwise, applies only to this
21 software file. Patent licenses, if any, provided herein
22 do not apply to combinations of this program with other
23 software, or any other product whatsoever.
24
25 You should have received a copy of the GNU Lesser General
26 Public License along with this program; if not, write the
27 Free Software Foundation, Inc., 51 Franklin Street - Fifth
28 Floor, Boston MA 02110-1301, USA.
29
30 */
31
32 #include <config.h>
33
34 #include <stddef.h> /* NULL */
35 #include <string.h> /* memset() strcmp() */
36
37 #ifdef HAVE_STDINT_H
38 #include <stdint.h> /* uintptr_t */
39 #endif
40
41 #ifdef DWARF_WITH_LIBELF
42 #ifdef HAVE_LIBELF_H
43 #include <libelf.h>
44 #endif /* HAVE_LIBELF_H */
45 #ifdef HAVE_LIBELF_LIBELF_H
46 #include <libelf/libelf.h>
47 #endif /* HAVE_LIBELF_LIBELF_H */
48 #ifdef HAVE_ELF_H /* does includes of elf.h libelf.h here. */
49 #include <elf.h>
50 #endif /* HAVE_ELF_H */
51 #endif /* DWARF_WITH_LIBELF */
52
53 #include "dwarf.h"
54 #include "libdwarf.h"
55 #include "dwarf_base_types.h"
56 #include "libdwarfp.h"
57 #include "dwarf_pro_incl.h"
58 #include "dwarf_pro_opaque.h"
59 #include "dwarf_pro_error.h"
60 #include "dwarf_pro_alloc.h"
61 #include "dwarf_pro_line.h"
62 #include "dwarf_memcpy_swap.h"
63 #include "dwarf_pro_section.h" /* for MAGIC_SECT_NO */
64 #include "dwarf_pro_reloc_symbolic.h"
65 #include "dwarf_pro_reloc_stream.h"
66 #include "dwarf_tsearch.h"
67 #include "dwarf_string.h"
68
69 #define IS_64BITPTR(dbg) ((dbg)->de_flags & DW_DLC_POINTER64 ? 1 : 0)
70 #define ISA_IA64(dbg) ((dbg)->de_flags & DW_DLC_ISA_IA64 ? 1 : 0)
71
72 struct isa_relocs_s {
73 const char *name_;
74 int reloc32_;
75 int reloc64_;
76 int segrel_; /* only used if IRIX */
77 };
78
79 /* Some of these may be the wrong relocation for DWARF
80 relocations. FIXME. Most will be unusable without
81 additional effort as they have not been tested.
82 */
83 #define R_MIPS_32 2
84 #define R_MIPS_64 18
85 #define R_MIPS_SCN_DISP 32
86 #define R_386_32 1
87 #define R_386_64 0 /* impossible */
88 #define R_X86_64_32 10
89 #define R_X86_64_64 1
90 #define R_SPARC_UA32 23
91 #define R_SPARC_UA64 54
92 #define R_ARM_ABS32 2
93 #define R_ARM_ABS64 0 /* impossible */
94 #define R_AARCH64_ABS32 258
95 #define R_AARCH64_ABS64 257
96 #define R_IA64_DIR32LSB 0x25
97 #define R_IA64_DIR64LSB 0x27
98 #define R_PPC_REL32 26
99 #define R_PPC_REL64 44
100 #define R_PPC64_REL32 R_PPC_REL32
101 #define R_PPC64_REL64 44
102
103 static struct isa_relocs_s isa_relocs[] = {
104 {"irix", R_MIPS_32,R_MIPS_64,R_MIPS_SCN_DISP},
105 {"mips", R_MIPS_32,R_MIPS_64,0},
106 {"x86", R_386_32, R_386_64,0},
107 {"x86_64",R_X86_64_32,R_X86_64_64,0},
108 {"ia64", R_IA64_DIR32LSB,R_IA64_DIR64LSB,0},
109 {"arm64", R_AARCH64_ABS32,R_AARCH64_ABS64,0},
110 {"arm", R_ARM_ABS32,R_ARM_ABS64,0},
111 {"ppc", R_PPC_REL32,R_PPC_REL64,0},
112 {"ppc64", R_PPC64_REL32,R_PPC64_REL64,0},
113 {"sparc", R_SPARC_UA32,R_SPARC_UA64,0},
114 /* The last entry MUST be all zeros. */
115 {0,0,0,0}
116 };
117
118 static int common_init(Dwarf_P_Debug dbg, Dwarf_Unsigned flags,
119 const char *abiname, const char *dwarf_version,
120 const char *extrainfo,
121 int *error_ret);
122
123 /* This function sets up a new dwarf producing region.
124 flags: Indicates type of access method, one of DW_DLC* macros
125 func(): Used to create a new object file, a call back function
126 errhand(): Error Handler provided by user
127 errarg: Argument to errhand()
128 error: returned error value */
129 /* We want the following to have an elf section
130 number that matches 'nothing' */
131 static struct Dwarf_P_Section_Data_s init_sect = {
132 MAGIC_SECT_NO, 0, 0, 0, 0
133 };
134 static struct Dwarf_P_Section_Data_s init_sect_debug_str = {
135 MAGIC_SECT_NO, 0, 0, 0, 0
136 };
137 static struct Dwarf_P_Section_Data_s init_sect_debug_line_str = {
138 MAGIC_SECT_NO, 0, 0, 0, 0
139 };
140
141 /* New April 2014.
142 Replaces all previous producer init functions.
143 It adds a string to select the relevant ABI/ISA and
144 a string defining the selected DWARF version to
145 output.
146 There are some overlaps between the flags and the ISA/ABI
147 string choices. ( it is neither strictly ABI nor strictly
148 ISA name, but a useful name for both.)
149 Generally, the function inteprets these
150 in a tolerant fashion, so inconsistencies in the
151 selections are not noticed...but they may have a surprising
152 effect.
153
154 The extra string is a way to allow new options without
155 changing the interface. The idea is the caller might
156 supply a list of such things as one string, comma-separated.
157 The interface is not intended to allow spaces or tabs in the
158 names, so don't do that :-)
159 If no extra strings are needed (none are defined initially)
160 then pass a NULL pointer or an empty string as the 'extra'
161 parameter.
162 */
163 int
164 dwarf_producer_init(Dwarf_Unsigned flags,
165 Dwarf_Callback_Func func,
166 Dwarf_Handler errhand,
167 Dwarf_Ptr errarg,
168 void * user_data,
169 const char *isa_name, /* See isa_reloc_s. */
170 const char *dwarf_version, /* V2 V3 V4 or V5. */
171 const char *extra, /* Extra input strings, comma separated. */
172 Dwarf_P_Debug *dbg_returned,
173 Dwarf_Error * error)
174 {
175 Dwarf_P_Debug dbg = 0;
176 int res = 0;
177 int err_ret = 0;
178 dbg = (Dwarf_P_Debug) _dwarf_p_get_alloc(NULL,
179 sizeof(struct Dwarf_P_Debug_s));
180 if (dbg == NULL) {
181 DWARF_P_DBG_ERROR(dbg, DW_DLE_DBG_ALLOC,
182 DW_DLV_ERROR);
183 }
184 memset((void *) dbg, 0, sizeof(struct Dwarf_P_Debug_s));
185 /* For the time being */
186 if (func == NULL) {
187 DWARF_P_DBG_ERROR(dbg, DW_DLE_NO_CALLBACK_FUNC,
188 DW_DLV_ERROR);
189 }
190 dbg->de_callback_func = func;
191 dbg->de_errhand = errhand;
192 dbg->de_errarg = errarg;
193 dbg->de_user_data = user_data;
194 res = common_init(dbg, flags,isa_name,dwarf_version,
195 extra,&err_ret);
196 if (res != DW_DLV_OK) {
197 DWARF_P_DBG_ERROR(dbg, err_ret, DW_DLV_ERROR);
198 }
199 *dbg_returned = dbg;
200 return DW_DLV_OK;
201 }
202
203 int
204 dwarf_pro_set_default_string_form(Dwarf_P_Debug dbg,
205 int form,
206 Dwarf_Error * error)
207 {
208 (void)error;
209 if (form != DW_FORM_string &&
210 form != DW_FORM_strp) {
211 _dwarf_p_error(dbg, error, DW_DLE_BAD_STRING_FORM);
212 return DW_DLV_ERROR;
213 }
214 dbg->de_debug_default_str_form = form;
215 return DW_DLV_OK;
216 }
217
218 static int
219 set_reloc_numbers(Dwarf_P_Debug dbg,
220 Dwarf_Unsigned flags,
221 const char *abiname)
222 {
223 struct isa_relocs_s *isap = 0;
224
225 (void)flags;
226 if (!abiname) {
227 return DW_DLV_NO_ENTRY;
228 }
229 for (isap = &isa_relocs[0]; ;isap++) {
230 if (!isap->name_) {
231 /* No more names known. Never found the one we wanted. */
232 return DW_DLV_NO_ENTRY;
233 }
234 if (!strcmp(abiname,isap->name_)) {
235 if (dbg->de_pointer_size == 4) {
236 dbg->de_ptr_reloc = isap->reloc32_;
237 } else {
238 dbg->de_ptr_reloc = isap->reloc64_;
239 }
240 if (dbg->de_dwarf_offset_size == 4) {
241 dbg->de_offset_reloc = isap->reloc32_;
242 } else {
243 dbg->de_offset_reloc = isap->reloc64_;
244 }
245 /* segrel only meaningful for IRIX, otherwise
246 harmless, unused. */
247 dbg->de_exc_reloc = isap->segrel_;
248 return DW_DLV_OK;
249 }
250 }
251 /* UNREACHED */
252 }
253
254 /* This is the Daniel J Bernstein hash function
255 originally posted to Usenet news.
256 http://en.wikipedia.org/wiki/List_of_hash_functions or
257 http://stackoverflow.com/questions/\
258 10696223/reason-for-5381-number-in-djb-hash-function).
259 See Also DWARF5 Section 7.33
260 */
261 static DW_TSHASHTYPE
262 _dwarf_string_hashfunc(const char *str)
263 {
264 DW_TSHASHTYPE up = 0;
265 DW_TSHASHTYPE hash = 5381;
266 int c = 0;
267
268 /* Extra parens suppress warning about assign in test. */
269 while ((c = *str++)) {
270 hash = hash * 33 + c ;
271 }
272 up = hash;
273 return up;
274 }
275 static DW_TSHASHTYPE
276 key_simple_string_hashfunc(const void *keyp)
277 {
278 struct Dwarf_P_debug_str_entry_s* mt =
279 (struct Dwarf_P_debug_str_entry_s*) keyp;
280 const char *str = 0;
281
282 if (mt->dse_has_table_offset) {
283 /* ASSERT: mt->dse_dbg->de_debug_str->ds_data not zero. */
284 str = (const char *)mt->dse_dbg->de_debug_str->ds_data +
285 mt->dse_table_offset;
286 } else {
287 /* ASSERT: dse_name != 0 */
288 str = (const char *)mt->dse_name;
289 }
290 return _dwarf_string_hashfunc(str);
291 }
292
293 static int
294 common_init(Dwarf_P_Debug dbg,
295 Dwarf_Unsigned flags,
296 const char *abiname,
297 const char *dwarf_version,
298 const char *extra,
299 int *err_ret)
300 {
301 unsigned int k = 0;
302 int res = 0;
303
304 dbg->de_version_magic_number = PRO_VERSION_MAGIC;
305 dbg->de_n_debug_sect = 0;
306 dbg->de_debug_sects = &init_sect;
307 dbg->de_debug_str = &init_sect_debug_str;
308 dbg->de_debug_line_str = &init_sect_debug_line_str;
309 dbg->de_current_active_section = &init_sect;
310 dbg->de_flags = flags;
311
312 /* DW_DLC_POINTER32 assumed. */
313 dbg->de_pointer_size = 4;
314 /* Standard DWARF 64bit offset, length field 12 bytes */
315 dbg->de_dwarf_offset_size = 4;
316 dbg->de_elf_offset_size = 4;
317 dbg->de_64bit_extension = 0;
318
319 dbg->de_big_endian = (dbg->de_flags&DW_DLC_TARGET_BIGENDIAN)?
320 TRUE:FALSE;
321 /* DW_DLC_POINTER64 is identical to DW_DLC_SIZE_64 */
322 if (dbg->de_flags & DW_DLC_POINTER64) {
323 dbg->de_pointer_size = 8;
324 }
325 if (dbg->de_flags & DW_DLC_OFFSET64) {
326 dbg->de_pointer_size = 8;
327 dbg->de_dwarf_offset_size = 4;
328 dbg->de_64bit_extension = 0;
329 /* When dwarf_offset_size == 8 then for
330 standard dwarf set
331 de_64bit_extension to 1. */
332 dbg->de_elf_offset_size = 8;
333 } else {
334 if (dbg->de_flags & DW_DLC_IRIX_OFFSET64) {
335 dbg->de_pointer_size = 8;
336 dbg->de_big_endian = TRUE;
337 dbg->de_dwarf_offset_size = 8;
338 dbg->de_64bit_extension = 0;
339 dbg->de_elf_offset_size = 8;
340 }
341 }
342
343 if (abiname && (!strcmp(abiname,"irix"))) {
344 dbg->de_irix_exc_augmentation = 1;
345 } else {
346 dbg->de_irix_exc_augmentation = 0;
347 }
348 /* We must set reloc numbers even if doing symbolic
349 relocations because we use the numbers up until
350 we are generating debug. A zero is interpreted
351 as no relocations. So ensure we have real
352 relocations. */
353 res = set_reloc_numbers(dbg,flags,abiname);
354 if (res != DW_DLV_OK) {
355 *err_ret = DW_DLE_BAD_ABINAME;
356 return DW_DLV_ERROR;
357 }
358 dbg->de_output_version = 2;
359 if (dwarf_version) {
360 if (!strcmp(dwarf_version,"V2")) {
361 dbg->de_output_version = 2;
362 } else if (!strcmp(dwarf_version,"V3")) {
363 dbg->de_output_version = 3;
364 } else if (!strcmp(dwarf_version,"V4")) {
365 dbg->de_output_version = 4;
366 } else if (!strcmp(dwarf_version,"V5")) {
367 dbg->de_output_version = 5;
368 } else {
369 *err_ret = DW_DLE_VERSION_STAMP_ERROR;
370 return DW_DLV_ERROR;
371 }
372 }
373 _dwarf_init_default_line_header_vals(dbg);
374 res = _dwarf_log_extra_flagstrings(dbg,extra,err_ret);
375 if (res == DW_DLV_ERROR) {
376 return res;
377 }
378
379 if (flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
380 dbg->de_relocation_record_size =
381 sizeof(struct Dwarf_Relocation_Data_s);
382 } else {
383 /* This is only going to work when the HOST == TARGET,
384 surely? */
385 #ifdef DWARF_WITH_LIBELF
386 #if HAVE_ELF64_GETEHDR
387 dbg->de_relocation_record_size =
388 ((dbg->de_pointer_size == 8)?
389 sizeof(REL64) : sizeof(REL32));
390 #else
391 dbg->de_relocation_record_size = sizeof(REL32);
392 #endif
393 #else /* DWARF_WITH_LIBELF */
394 *err_ret = DW_DLE_NO_STREAM_RELOC_SUPPORT;
395 return DW_DLV_ERROR;
396 #endif /* DWARF_WITH_LIBELF */
397 }
398
399 /* For .debug_str creation. */
400 dwarf_initialize_search_hash(&dbg->de_debug_str_hashtab,
401 key_simple_string_hashfunc,0);
402 dbg->de_debug_default_str_form = DW_FORM_string;
403 dwarf_initialize_search_hash(&dbg->de_debug_line_str_hashtab,
404 key_simple_string_hashfunc,0);
405
406 if (dbg->de_dwarf_offset_size == 8) {
407 if (dbg->de_output_version <= 3) {
408 dbg->de_ar_data_attribute_form = DW_FORM_data8;
409 } else {
410 dbg->de_ar_data_attribute_form = DW_FORM_sec_offset;
411 }
412 dbg->de_ar_ref_attr_form = DW_FORM_ref8;
413 } else {
414 if (dbg->de_output_version <= 3) {
415 dbg->de_ar_data_attribute_form = DW_FORM_data4;
416 } else {
417 dbg->de_ar_data_attribute_form = DW_FORM_sec_offset;
418 }
419 dbg->de_ar_ref_attr_form = DW_FORM_ref4;
420 }
421
422 if (flags & DW_DLC_SYMBOLIC_RELOCATIONS) {
423 dbg->de_relocate_by_name_symbol =
424 _dwarf_pro_reloc_name_symbolic;
425 dbg->de_relocate_pair_by_symbol =
426 _dwarf_pro_reloc_length_symbolic;
427 dbg->de_transform_relocs_to_disk =
428 _dwarf_symbolic_relocs_to_disk;
429 } else {
430 #ifdef DWARF_WITH_LIBELF
431 if (IS_64BITPTR(dbg)) {
432 dbg->de_relocate_by_name_symbol =
433 _dwarf_pro_reloc_name_stream64;
434 } else {
435 dbg->de_relocate_by_name_symbol =
436 _dwarf_pro_reloc_name_stream32;
437 }
438 dbg->de_relocate_pair_by_symbol = 0;
439 dbg->de_transform_relocs_to_disk =
440 _dwarf_stream_relocs_to_disk;
441 #else /* DWARF_WITH_LIBELF */
442 *err_ret = DW_DLE_NO_STREAM_RELOC_SUPPORT;
443 return DW_DLV_ERROR;
444 #endif /* DWARF_WITH_LIBELF */
445 }
446 for (k = 0; k < NUM_DEBUG_SECTIONS; ++k) {
447
448 Dwarf_P_Per_Reloc_Sect prel = &dbg->de_reloc_sect[k];
449
450 prel->pr_slots_per_block_to_alloc = DEFAULT_SLOTS_PER_BLOCK;
451 }
452 /* First assume host, target same endianness FIXME */
453 dbg->de_same_endian = 1;
454 dbg->de_copy_word = _dwarf_memcpy_noswap_bytes;
455 #ifdef WORDS_BIGENDIAN
456 /* host is big endian, so what endian is target? */
457 if (flags & DW_DLC_TARGET_LITTLEENDIAN) {
458 dbg->de_same_endian = 0;
459 dbg->de_copy_word = _dwarf_memcpy_swap_bytes;
460 }
461 #else /* little endian */
462 /* host is little endian, so what endian is target? */
463 if (flags & DW_DLC_TARGET_BIGENDIAN) {
464 dbg->de_same_endian = 0;
465 dbg->de_copy_word = _dwarf_memcpy_swap_bytes;
466 }
467 #endif /* !WORDS_BIGENDIAN */
468 return DW_DLV_OK;
469 }