1 /*
2 Copyright (C) 2000,2002,2003,2004,2005 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2008-2010 Arxan Technologies, Inc. All Rights Reserved.
4 Portions Copyright (C) 2009-2019 David Anderson. All Rights Reserved.
5 Portions Copyright (C) 2010-2012 SN Systems Ltd. All Rights Reserved.
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of version 2.1 of the GNU Lesser General Public License
9 as published by the Free Software Foundation.
10
11 This program is distributed in the hope that it would be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15 Further, this software is distributed without any warranty that it is
16 free of the rightful claim of any third person regarding infringement
17 or the like. Any license provided herein, whether implied or
18 otherwise, applies only to this software file. Patent licenses, if
19 any, provided herein do not apply to combinations of this program with
20 other software, or any other product whatsoever.
21
22 You should have received a copy of the GNU Lesser General Public
23 License along with this program; if not, write the Free Software
24 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
25 USA.
26
27 */
28
29 #include "config.h"
30 #include <stdio.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <string.h>
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif /* HAVE_STDLIB_H */
37 #ifdef HAVE_MALLOC_H
38 /* Useful include for some Windows compilers. */
39 #include <malloc.h>
40 #endif /* HAVE_MALLOC_H */
41 #include "dwarf_incl.h"
42 #include "dwarf_alloc.h"
43 #include "dwarf_error.h"
44 #include "dwarf_util.h"
45 #include "memcpy_swap.h"
46 #include "dwarf_harmless.h"
47 #include "dwarfstring.h"
48
49 /* For consistency, use the HAVE_LIBELF_H symbol */
50 #ifdef HAVE_LIBELF_H
51 #include <libelf.h>
52 #else
53 #ifdef HAVE_LIBELF_LIBELF_H
54 #include <libelf/libelf.h>
55 #endif
56 #endif
57 #ifdef HAVE_ZLIB
58 #include "zlib.h"
59 #endif
60
61 #ifndef ELFCOMPRESS_ZLIB
62 #define ELFCOMPRESS_ZLIB 1
63 #endif
64
65 /* If your mingw elf.h is missing SHT_RELA and you do not
66 need SHT_RELA support
67 this define should work for you.
68 It is the elf value, hopefully it will
69 not cause trouble. If does not work, try -1
70 or something else
71 and let us know what works. */
72 #ifndef SHT_RELA
73 #define SHT_RELA 4
74 #endif
75 #ifndef SHT_REL
76 #define SHT_REL 9
77 # endif
78 /* For COMDAT GROUPS. Guarantees we can compile. We hope. */
79 #ifndef SHT_GROUP
80 #define SHT_GROUP 17
81 #endif
82
83 #ifndef SHF_COMPRESSED
84 /* This from ubuntu xenial. Is in top of trunk binutils
85 as of February 2016. Elf Section Flag */
86 #define SHF_COMPRESSED (1 << 11)
87 #endif
88
89
90 #define DWARF_DBG_ERROR(dbg,errval,retval) \
91 _dwarf_error(dbg, error, errval); return(retval);
92
93 #define FALSE 0
94 #define TRUE 1
95
96 /* Global definition of the function pointer type, typedef
97 in dwarf_opaque.h */
98 _dwarf_get_elf_flags_func_ptr_type _dwarf_get_elf_flags_func_ptr;
99
100 /* This static is copied to the dbg on dbg init
101 so that the static need not be referenced at
102 run time, preserving better locality of
103 reference.
104 Value is 0 means do the string check.
105 Value non-zero means do not do the check.
106 */
107 static Dwarf_Small _dwarf_assume_string_in_bounds;
108 static Dwarf_Small _dwarf_apply_relocs = 1;
109
110 /* Call this after calling dwarf_init but before doing anything else.
111 It applies to all objects, not just the current object. */
112 int
dwarf_set_reloc_application(int apply)113 dwarf_set_reloc_application(int apply)
114 {
115 int oldval = _dwarf_apply_relocs;
116 _dwarf_apply_relocs = apply;
117 return oldval;
118 }
119
120 int
dwarf_set_stringcheck(int newval)121 dwarf_set_stringcheck(int newval)
122 {
123 int oldval = _dwarf_assume_string_in_bounds;
124
125 _dwarf_assume_string_in_bounds = newval;
126 return oldval;
127 }
128
129 static int
startswith(const char * input,char * ckfor)130 startswith(const char * input, char* ckfor)
131 {
132 size_t cklen = strlen(ckfor);
133
134 if (! strncmp(input,ckfor,cklen)) {
135 return TRUE;
136 }
137 return FALSE;
138 }
139 #if 0
140 static int
141 endswith(const char * input, char* ckfor)
142 {
143 size_t inlen = strlen(input);
144 size_t endlen = strlen(ckfor);
145 const char * endck = 0;
146
147 if (endlen > inlen) {
148 return FALSE;
149 }
150 endck = input+inlen - endlen;
151
152 if (! strcmp(endck,ckfor) ) {
153 return TRUE;
154 }
155 return FALSE;
156 }
157 #endif
158
159 /* Unifies the basic duplicate/empty testing and section
160 data setting to one place. */
161 static int
get_basic_section_data(Dwarf_Debug dbg,struct Dwarf_Section_s * secdata,struct Dwarf_Obj_Access_Section_s * doas,Dwarf_Half section_index,unsigned group_number,Dwarf_Error * error,int duperr,int emptyerr)162 get_basic_section_data(Dwarf_Debug dbg,
163 struct Dwarf_Section_s *secdata,
164 struct Dwarf_Obj_Access_Section_s *doas,
165 Dwarf_Half section_index,
166 unsigned group_number,
167 Dwarf_Error* error,
168 int duperr, int emptyerr )
169 {
170 /* There is an elf convention that section index 0 is reserved,
171 and that section is always empty.
172 Non-elf object formats must honor that by ensuring that
173 (when they assign numbers to 'sections' or 'section-like-things')
174 they never assign a real section section-number 0 to dss_index. */
175 if (secdata->dss_index != 0) {
176 DWARF_DBG_ERROR(dbg, duperr, DW_DLV_ERROR);
177 }
178 if (doas->size == 0) {
179 /* As of 2018 it seems impossible to detect
180 (via dwarfdump) whether emptyerr has any
181 practical effect, whether TRUE or FALSE. */
182 if (emptyerr == 0 ) {
183 /* Allow empty section. */
184 return DW_DLV_OK;
185 }
186 /* Know no reason to allow section */
187 DWARF_DBG_ERROR(dbg, emptyerr, DW_DLV_ERROR);
188 }
189 secdata->dss_index = section_index;
190 secdata->dss_size = doas->size;
191 secdata->dss_group_number = group_number;
192 secdata->dss_addr = doas->addr;
193 secdata->dss_link = doas->link;
194 secdata->dss_entrysize = doas->entrysize;
195 if (_dwarf_get_elf_flags_func_ptr) {
196 /* We do this so we do not need to update the public struct
197 Dwarf_Obj_Access_Section_s and thereby cause
198 binary and source incompatibility. */
199 Dwarf_Unsigned flags = 0;
200 Dwarf_Unsigned addralign = 0;
201 int res = 0;
202 int interr = 0;
203 struct Dwarf_Obj_Access_Interface_s *o = 0;
204
205 o = dbg->de_obj_file;
206 res = _dwarf_get_elf_flags_func_ptr(
207 o->object, section_index,
208 &flags,&addralign,
209 &interr);
210 if (res == DW_DLV_ERROR) {
211 /* Should never get here. */
212 DWARF_DBG_ERROR(dbg, interr, DW_DLV_ERROR);
213 }
214 if (res == DW_DLV_NO_ENTRY) {
215 return res;
216 }
217 secdata->dss_flags = flags;
218 secdata->dss_addralign = addralign;
219 if (flags & SHF_COMPRESSED) {
220 secdata->dss_shf_compressed = TRUE;
221 }
222 /* We are not looking at section bytes so we
223 do not know if the first 4 bytes are ZLIB */
224 }
225 return DW_DLV_OK;
226 }
227
228
229 static void
add_relx_data_to_secdata(struct Dwarf_Section_s * secdata,struct Dwarf_Obj_Access_Section_s * doas,Dwarf_Half section_index,int is_rela)230 add_relx_data_to_secdata( struct Dwarf_Section_s *secdata,
231 struct Dwarf_Obj_Access_Section_s *doas,
232 Dwarf_Half section_index,int is_rela)
233 {
234 secdata->dss_reloc_index = section_index;
235 secdata->dss_reloc_size = doas->size;
236 secdata->dss_reloc_entrysize = doas->entrysize;
237 secdata->dss_reloc_addr = doas->addr;
238 secdata->dss_reloc_symtab = doas->link;
239 secdata->dss_reloc_link = doas->link;
240 secdata->dss_is_rela = is_rela;
241 }
242
243
244
245 /* Used to add the specific information for a debug related section
246 Called on each section of interest by section name.
247 DWARF_MAX_DEBUG_SECTIONS must be large enough to allow
248 that all sections of interest fit in the table.
249 returns DW_DLV_ERROR or DW_DLV_OK.
250 */
251 static int
add_debug_section_info(Dwarf_Debug dbg,const char * name,const char * standard_section_name,unsigned obj_sec_num,struct Dwarf_Section_s * secdata,unsigned groupnum,int duperr,int emptyerr,int have_dwarf,int havezdebug,int * err)252 add_debug_section_info(Dwarf_Debug dbg,
253 /* Name as seen in object file. */
254 const char *name,
255 const char *standard_section_name,
256 unsigned obj_sec_num,
257 struct Dwarf_Section_s *secdata,
258 unsigned groupnum,
259 /* The have_dwarf flag is a somewhat imprecise
260 way to determine if there is at least one 'meaningful'
261 DWARF information section present in the object file.
262 If not set on some section we claim (later) that there
263 is no DWARF info present. see 'foundDwarf' in this file */
264 int duperr,int emptyerr,int have_dwarf,
265 int havezdebug,
266 int *err)
267 {
268 unsigned total_entries = dbg->de_debug_sections_total_entries;
269 if (secdata->dss_is_in_use) {
270 *err = duperr;
271 return DW_DLV_ERROR;
272 }
273 if (total_entries < DWARF_MAX_DEBUG_SECTIONS) {
274 struct Dwarf_dbg_sect_s *debug_section =
275 &dbg->de_debug_sections[total_entries];
276 secdata->dss_is_in_use = TRUE;
277 debug_section->ds_name = name;
278 debug_section->ds_number = obj_sec_num;
279 debug_section->ds_secdata = secdata;
280 debug_section->ds_groupnumber = groupnum;
281 secdata->dss_name = name; /* Actual name from object file. */
282 secdata->dss_standard_name = standard_section_name;
283 secdata->dss_number = obj_sec_num;
284 secdata->dss_zdebug_requires_decompress = havezdebug;
285 /* We don't yet know about SHF_COMPRESSED */
286 debug_section->ds_duperr = duperr;
287 debug_section->ds_emptyerr = emptyerr;
288 debug_section->ds_have_dwarf = have_dwarf;
289 debug_section->ds_have_zdebug = havezdebug;
290 ++dbg->de_debug_sections_total_entries;
291 return DW_DLV_OK;
292 }
293 /* This represents a bug in libdwarf.
294 Mis-setup-DWARF_MAX_DEBUG_SECTIONS.
295 Or possibly a use of section groups that is
296 not supported. */
297 *err = DW_DLE_TOO_MANY_DEBUG;
298 return DW_DLV_ERROR;
299 }
300
301
302 #if 0
303 static void
304 dump_bytes(const char *msg,Dwarf_Small * start, long len)
305 {
306 Dwarf_Small *end = start + len;
307 Dwarf_Small *cur = start;
308
309 printf("dump_bytes: %s ",msg);
310 for (; cur < end; cur++) {
311 printf("%02x",*cur);
312 }
313 printf("\n");
314 }
315
316 static int
317 all_sig8_bits_zero(Dwarf_Sig8 *val)
318 {
319 unsigned u = 0;
320 for( ; u < sizeof(*val); ++u) {
321 if (val->signature[u] != 0) {
322 return FALSE;
323 }
324 }
325 return TRUE;
326 }
327 #endif
328
329
330 /* Return DW_DLV_OK etc. */
331 static int
set_up_section(Dwarf_Debug dbg,const char * secname,const char * sec_standard_name,unsigned obj_sec_num,const char * targname,unsigned groupnum_of_sec,struct Dwarf_Section_s * secdata,int duperr,int emptyerr,int have_dwarf,int * err)332 set_up_section(Dwarf_Debug dbg,
333 /* Section name from object format.
334 Might start with .zdebug not .debug if compressed section. */
335 const char *secname,
336 /* Standard section name, such as .debug_info */
337 const char *sec_standard_name,
338 /* Section number from object format */
339 unsigned obj_sec_num,
340 /* The name associated with this secdata in libdwarf */
341 const char *targname,
342 /* DW_GROUPNUMBER_ANY or BASE or DWO or some other group num */
343 unsigned groupnum_of_sec,
344 struct Dwarf_Section_s *secdata,
345 int duperr,int emptyerr,int have_dwarf,
346 int *err)
347 {
348 /* Here accomodate the .debug or .zdebug version, (and of
349 course non- .debug too, but those never zlib) .
350 SECNAMEMAX should be a little bigger than any section
351 name we care about as possibly compressed, which
352 is to say bigger than any standard section name. */
353 #define SECNAMEMAX 30
354 int secnamelen = strlen(secname);
355 /* static const char *dprefix = ".debug_"; */
356 #define DPREFIXLEN 7
357 static const char *zprefix = ".zdebug_";
358 #define ZPREFIXLEN 8
359 int havezdebug = FALSE;
360 int namesmatch = FALSE;
361
362 /* For example, if the secname is .zdebug_info
363 we update the finaltargname to .debug_info
364 to match with the particular (known, predefined)
365 object section name.
366 We add one character, so check
367 to see if it will, in the end, fit.
368 See the SET_UP_SECTION macro. */
369
370 if(secnamelen >= SECNAMEMAX) {
371 /* This is not the target section.
372 our caller will keep looking. */
373 return DW_DLV_NO_ENTRY;
374 }
375 if((secnamelen+1) < SECNAMEMAX &&
376 !strncmp(secname,zprefix,ZPREFIXLEN) &&
377 !strcmp(secname+ZPREFIXLEN,targname+DPREFIXLEN)) {
378 /* zprefix version matches the object section
379 name so the section is compressed and is
380 the section this targname applies to. */
381 havezdebug = TRUE;
382 namesmatch = TRUE;
383 } else if (!strcmp(secname,targname)) {
384 namesmatch = TRUE;
385 }
386 #undef ZPREFIXLEN
387 #undef DPREFIXLEN
388 #undef SECNAMEMAX
389 if(!namesmatch) {
390 /* This is not the target section.
391 our caller will keep looking. */
392 return DW_DLV_NO_ENTRY;
393 }
394
395 /* SETUP_SECTION. See also BUILDING_SECTIONS, BUILDING_MAP */
396 {
397 /* The section name is a match with targname, or
398 the .zdebug version of targname. */
399 int sectionerr = 0;
400
401 sectionerr = add_debug_section_info(dbg,secname,
402 sec_standard_name,
403 obj_sec_num,
404 secdata,
405 groupnum_of_sec,
406 duperr,emptyerr, have_dwarf,
407 havezdebug,err);
408 if (sectionerr != DW_DLV_OK) {
409 /* *err is set already */
410 return sectionerr;
411 }
412 }
413 return DW_DLV_OK;
414 }
415
416 #define SET_UP_SECTION(mdbg,mname,mtarg,mgrp,minfo,me1,me2,mdw,mer) \
417 { \
418 int lerr = 0; \
419 lerr = set_up_section(mdbg, \
420 mname, /* actual section name */ \
421 mtarg, /* std section name */ \
422 /* scn_number from macro use context */ \
423 scn_number,mtarg,mgrp, \
424 minfo, \
425 me1,me2,mdw,mer); \
426 if (lerr != DW_DLV_NO_ENTRY) { \
427 return lerr; \
428 } /* else fall through. */ \
429 }
430
431 /* If running this long set of tests is slow
432 enough to matter one could set up a local
433 tsearch tree with all this content and search
434 it instead of this set of sequential tests.
435 Or use a switch(){} here with a search tree
436 to to turn name into index for the switch(). */
437 static int
enter_section_in_de_debug_sections_array(Dwarf_Debug dbg,const char * scn_name,unsigned scn_number,unsigned group_number,int * err)438 enter_section_in_de_debug_sections_array(Dwarf_Debug dbg,
439 const char *scn_name,
440 /* This is the number of the section in the object file. */
441 unsigned scn_number,
442 unsigned group_number,
443 int *err)
444 {
445 /* Setup the table that contains the basic information about the
446 sections that are DWARF related. The entries are very unlikely
447 to change very often. */
448 SET_UP_SECTION(dbg,scn_name,".debug_info",
449 group_number,
450 &dbg->de_debug_info,
451 DW_DLE_DEBUG_INFO_DUPLICATE,DW_DLE_DEBUG_INFO_NULL,
452 TRUE,err);
453 SET_UP_SECTION(dbg,scn_name,".debug_info.dwo",
454 DW_GROUPNUMBER_DWO,
455 &dbg->de_debug_info,
456 DW_DLE_DEBUG_INFO_DUPLICATE,DW_DLE_DEBUG_INFO_NULL,
457 TRUE,err);
458 SET_UP_SECTION(dbg,scn_name,".debug_types",
459 group_number,
460 &dbg->de_debug_types,
461 DW_DLE_DEBUG_TYPES_DUPLICATE,DW_DLE_DEBUG_TYPES_NULL,
462 TRUE,err);
463 SET_UP_SECTION(dbg,scn_name,".debug_types.dwo",
464 DW_GROUPNUMBER_DWO,
465 &dbg->de_debug_types,
466 DW_DLE_DEBUG_TYPES_DUPLICATE,DW_DLE_DEBUG_TYPES_NULL,
467 TRUE,err);
468 SET_UP_SECTION(dbg,scn_name,".debug_abbrev",
469 group_number,
470 &dbg->de_debug_abbrev, /*03*/
471 DW_DLE_DEBUG_ABBREV_DUPLICATE,DW_DLE_DEBUG_ABBREV_NULL,
472 TRUE,err);
473 SET_UP_SECTION(dbg,scn_name,".debug_abbrev.dwo",
474 DW_GROUPNUMBER_DWO,
475 &dbg->de_debug_abbrev, /*03*/
476 DW_DLE_DEBUG_ABBREV_DUPLICATE,DW_DLE_DEBUG_ABBREV_NULL,
477 TRUE,err);
478 SET_UP_SECTION(dbg,scn_name,".debug_aranges",
479 group_number,
480 &dbg->de_debug_aranges,
481 DW_DLE_DEBUG_ARANGES_DUPLICATE,0,
482 FALSE,err);
483 SET_UP_SECTION(dbg,scn_name,".debug_line",
484 group_number,
485 &dbg->de_debug_line,
486 DW_DLE_DEBUG_LINE_DUPLICATE,0,
487 TRUE,err);
488 /* DWARF5 */
489 SET_UP_SECTION(dbg,scn_name,".debug_line_str",
490 group_number,
491 &dbg->de_debug_line_str,
492 DW_DLE_DEBUG_LINE_DUPLICATE,0,
493 FALSE,err);
494 SET_UP_SECTION(dbg,scn_name,".debug_line.dwo",
495 DW_GROUPNUMBER_DWO,
496 &dbg->de_debug_line,
497 DW_DLE_DEBUG_LINE_DUPLICATE,0,
498 TRUE,err);
499 SET_UP_SECTION(dbg,scn_name,".debug_frame",
500 group_number,
501 &dbg->de_debug_frame,
502 DW_DLE_DEBUG_FRAME_DUPLICATE,0,
503 TRUE,err);
504 /* gnu egcs-1.1.2 data */
505 SET_UP_SECTION(dbg,scn_name,".eh_frame",
506 group_number,
507 &dbg->de_debug_frame_eh_gnu,
508 DW_DLE_DEBUG_FRAME_DUPLICATE,0,
509 TRUE,err);
510 SET_UP_SECTION(dbg,scn_name,".debug_loc",
511 group_number,
512 &dbg->de_debug_loc,
513 DW_DLE_DEBUG_LOC_DUPLICATE,0,
514 FALSE,err);
515 SET_UP_SECTION(dbg,scn_name,".debug_loc.dwo",
516 DW_GROUPNUMBER_DWO,
517 &dbg->de_debug_loc,
518 DW_DLE_DEBUG_LOC_DUPLICATE,0,
519 FALSE,err);
520 SET_UP_SECTION(dbg,scn_name,".debug_pubnames",
521 group_number,
522 &dbg->de_debug_pubnames,
523 DW_DLE_DEBUG_PUBNAMES_DUPLICATE,0,
524 FALSE,err);
525 SET_UP_SECTION(dbg,scn_name,".debug_str",
526 group_number,
527 &dbg->de_debug_str,
528 DW_DLE_DEBUG_STR_DUPLICATE,0,
529 FALSE,err);
530 SET_UP_SECTION(dbg,scn_name,".debug_str.dwo",
531 DW_GROUPNUMBER_DWO,
532 &dbg->de_debug_str,
533 DW_DLE_DEBUG_STR_DUPLICATE,0,
534 FALSE,err);
535 /* Section new in DWARF3. */
536 SET_UP_SECTION(dbg,scn_name,".debug_pubtypes",
537 group_number,
538 &dbg->de_debug_pubtypes,
539 /*13*/ DW_DLE_DEBUG_PUBTYPES_DUPLICATE,0,
540 FALSE,err);
541 /* DWARF5 */
542 SET_UP_SECTION(dbg,scn_name,".debug_names",
543 group_number,
544 &dbg->de_debug_names,
545 /*13*/ DW_DLE_DEBUG_NAMES_DUPLICATE,0,
546 FALSE,err);
547 /* DWARF5 */
548 SET_UP_SECTION(dbg,scn_name,".debug_loclists",
549 group_number,
550 &dbg->de_debug_loclists,
551 /*13*/ DW_DLE_DEBUG_LOClISTS_DUPLICATE,0,
552 FALSE,err);
553 /* DWARF5 */
554 SET_UP_SECTION(dbg,scn_name,".debug_loclists.dwo",
555 DW_GROUPNUMBER_DWO,
556 &dbg->de_debug_loclists,
557 /*13*/ DW_DLE_DEBUG_LOClISTS_DUPLICATE,0,
558 FALSE,err);
559 /* DWARF5 */
560 SET_UP_SECTION(dbg,scn_name,".debug_rnglists",
561 group_number,
562 &dbg->de_debug_rnglists,
563 /*13*/ DW_DLE_DEBUG_RNGLISTS_DUPLICATE,0,
564 FALSE,err);
565 /* DWARF5 */
566 SET_UP_SECTION(dbg,scn_name,".debug_rnglists.dwo",
567 DW_GROUPNUMBER_DWO,
568 &dbg->de_debug_rnglists,
569 /*13*/ DW_DLE_DEBUG_RNGLISTS_DUPLICATE,0,
570 FALSE,err);
571 /* DWARF5 */
572 SET_UP_SECTION(dbg,scn_name,".debug_str_offsets",
573 group_number,
574 &dbg->de_debug_str_offsets,
575 DW_DLE_DEBUG_STR_OFFSETS_DUPLICATE,0,
576 FALSE,err);
577 /* DWARF5 */
578 SET_UP_SECTION(dbg,scn_name,".debug_str_offsets.dwo",
579 DW_GROUPNUMBER_DWO,
580 &dbg->de_debug_str_offsets,
581 DW_DLE_DEBUG_STR_OFFSETS_DUPLICATE,0,
582 FALSE,err);
583
584 /* SGI IRIX-only. */
585 SET_UP_SECTION(dbg,scn_name,".debug_funcnames",
586 group_number,
587 &dbg->de_debug_funcnames,
588 /*11*/ DW_DLE_DEBUG_FUNCNAMES_DUPLICATE,0,
589 FALSE,err);
590 /* SGI IRIX-only, created years before DWARF3. Content
591 essentially identical to .debug_pubtypes. */
592 SET_UP_SECTION(dbg,scn_name,".debug_typenames",
593 group_number,
594 &dbg->de_debug_typenames,
595 /*12*/ DW_DLE_DEBUG_TYPENAMES_DUPLICATE,0,
596 FALSE,err);
597 /* SGI IRIX-only. */
598 SET_UP_SECTION(dbg,scn_name,".debug_varnames",
599 group_number,
600 &dbg->de_debug_varnames,
601 DW_DLE_DEBUG_VARNAMES_DUPLICATE,0,
602 FALSE,err);
603 /* SGI IRIX-only. */
604 SET_UP_SECTION(dbg,scn_name,".debug_weaknames",
605 group_number,
606 &dbg->de_debug_weaknames,
607 DW_DLE_DEBUG_WEAKNAMES_DUPLICATE,0,
608 FALSE,err);
609
610 SET_UP_SECTION(dbg,scn_name,".debug_macinfo",
611 group_number,
612 &dbg->de_debug_macinfo,
613 DW_DLE_DEBUG_MACINFO_DUPLICATE,0,
614 TRUE,err);
615 /* ".debug_macinfo.dwo" is not allowed. */
616
617
618 /* DWARF5 */
619 SET_UP_SECTION(dbg,scn_name,".debug_macro",
620 group_number,
621 &dbg->de_debug_macro,
622 DW_DLE_DEBUG_MACRO_DUPLICATE,0,
623 TRUE,err);
624 /* DWARF5 */
625 SET_UP_SECTION(dbg,scn_name,".debug_macro.dwo",
626 DW_GROUPNUMBER_DWO,
627 &dbg->de_debug_macro,
628 DW_DLE_DEBUG_MACRO_DUPLICATE,0,
629 TRUE,err);
630 SET_UP_SECTION(dbg,scn_name,".debug_ranges",
631 group_number,
632 &dbg->de_debug_ranges,
633 DW_DLE_DEBUG_RANGES_DUPLICATE,0,
634 TRUE,err);
635 /* No .debug_ranges.dwo allowed. */
636
637 /* New DWARF5 */
638 SET_UP_SECTION(dbg,scn_name,".debug_sup",
639 group_number,
640 &dbg->de_debug_sup,
641 DW_DLE_DEBUG_SUP_DUPLICATE,0,
642 TRUE,err);
643 /* No .debug_sup.dwo allowed. */
644
645 /* .symtab and .strtab have to be in any group. */
646 SET_UP_SECTION(dbg,scn_name,".symtab",
647 group_number,
648 &dbg->de_elf_symtab,
649 DW_DLE_DEBUG_SYMTAB_ERR,0,
650 FALSE,err);
651 SET_UP_SECTION(dbg,scn_name,".strtab",
652 group_number,
653 &dbg->de_elf_strtab,
654 DW_DLE_DEBUG_STRTAB_ERR,0,
655 FALSE,err);
656
657 /* New DWARF5 */
658 SET_UP_SECTION(dbg,scn_name,".debug_addr",
659 group_number,
660 &dbg->de_debug_addr,
661 DW_DLE_DEBUG_ADDR_DUPLICATE,0,
662 TRUE,err);
663 /* No .debug_addr.dwo allowed. */
664
665 /* gdb added this. */
666 SET_UP_SECTION(dbg,scn_name,".gdb_index",
667 group_number,
668 &dbg->de_debug_gdbindex,
669 DW_DLE_DUPLICATE_GDB_INDEX,0,
670 FALSE,err);
671
672 /* New DWARF5 */
673 SET_UP_SECTION(dbg,scn_name,".debug_names",
674 group_number,
675 &dbg->de_debug_names,
676 DW_DLE_DEBUG_NAMES_DUPLICATE,0,
677 FALSE,err);
678 /* No .debug_names.dwo allowed. */
679
680 /* gdb added this in DW4. It is in standard DWARF5 */
681 SET_UP_SECTION(dbg,scn_name,".debug_cu_index",
682 DW_GROUPNUMBER_DWO,
683 &dbg->de_debug_cu_index,
684 DW_DLE_DUPLICATE_CU_INDEX,0,
685 FALSE,err);
686 /* gdb added this in DW4. It is in standard DWARF5 */
687 SET_UP_SECTION(dbg,scn_name,".debug_tu_index",
688 DW_GROUPNUMBER_DWO,
689 &dbg->de_debug_tu_index,
690 DW_DLE_DUPLICATE_TU_INDEX,0,
691 FALSE,err);
692
693 /* GNU added this. It is not part of DWARF */
694 SET_UP_SECTION(dbg,scn_name,".gnu_debuglink",
695 DW_GROUPNUMBER_DWO,
696 &dbg->de_gnu_debuglink,
697 DW_DLE_DUPLICATE_GNU_DEBUGLINK,0,
698 FALSE,err);
699
700 /* GNU added this. It is not part of DWARF */
701 SET_UP_SECTION(dbg,scn_name,".note.gnu.build-id",
702 DW_GROUPNUMBER_DWO,
703 &dbg->de_note_gnu_buildid,
704 DW_DLE_DUPLICATE_GNU_DEBUGLINK,0,
705 FALSE,err);
706 return DW_DLV_NO_ENTRY;
707 }
708 static int
is_section_name_known_already(Dwarf_Debug dbg,const char * scn_name)709 is_section_name_known_already(Dwarf_Debug dbg, const char *scn_name)
710 {
711 unsigned i = 0;
712 for ( ; i < dbg->de_debug_sections_total_entries; ++i) {
713 struct Dwarf_dbg_sect_s *section = &dbg->de_debug_sections[i];
714 if (!strcmp(scn_name, section->ds_name)) {
715 /* The caller will declare this a duplicate, an error. */
716 return DW_DLV_OK;
717 }
718 }
719 /* This is normal, we expect we've not accepted scn_name already. */
720 return DW_DLV_NO_ENTRY;
721 }
722
723 /* Given an Elf ptr, set up dbg with pointers
724 to all the Dwarf data sections.
725 Return NULL on error.
726
727 This function is also responsible for determining
728 whether the given object contains Dwarf information
729 or not. The test currently used is that it contains
730 either a .debug_info or a .debug_frame section. If
731 not, it returns DW_DLV_NO_ENTRY causing dwarf_init() also to
732 return DW_DLV_NO_ENTRY. Earlier, we had thought of using only
733 the presence/absence of .debug_info to test, but we
734 added .debug_frame since there could be stripped objects
735 that have only a .debug_frame section for exception
736 processing.
737 DW_DLV_NO_ENTRY or DW_DLV_OK or DW_DLV_ERROR
738
739 This does not allow for section-groups in object files,
740 for which many .debug_info (and other DWARF) sections may exist.
741
742 We process. .rela (SHT_RELA) and .rel (SHT_REL)
743 sections because with .rela the referencing section
744 offset value is zero whereas with .rel the
745 referencing section value is already correct for
746 the object itself. In other words, we do it because
747 of the definition of .rela relocations in Elf.
748
749 However! In some cases clang emits a .rel section (at least
750 for .rel.debug_info) where symtab entries have an st_value
751 that must be treated like an addend: the compiler did not
752 bother to backpatch the DWARF information for these.
753 */
754
755
756 /* These help us ignore some sections that are
757 irrelevant to libdwarf. Maybe should use a hash
758 table instead of sequential search? */
759 int
_dwarf_ignorethissection(const char * scn_name)760 _dwarf_ignorethissection(const char *scn_name) {
761 if(!strcmp(scn_name,".bss")) {
762 return TRUE;
763 }
764 if(!strcmp(scn_name,".comment")) {
765 return TRUE;
766 }
767 if(!strcmp(scn_name,".sbss")) {
768 return TRUE;
769 }
770 if(!strcmp(scn_name,".jcr")) {
771 return TRUE;
772 }
773 if(!strcmp(scn_name,".init")) {
774 return TRUE;
775 }
776 if(!strcmp(scn_name,".fini_array")) {
777 return TRUE;
778 }
779 if(!strcmp(scn_name,".fini")) {
780 return TRUE;
781 }
782 if(!strcmp(scn_name,".fini_array")) {
783 return TRUE;
784 }
785 if(!strcmp(scn_name,".interp")) {
786 return TRUE;
787 }
788 if(!strcmp(scn_name,".text")) {
789 return TRUE;
790 }
791 if(!strcmp(scn_name,".rela.text")) {
792 return TRUE;
793 }
794 if(!strcmp(scn_name,".rel.text")) {
795 return TRUE;
796 }
797
798 if(!strcmp(scn_name,".plt")) {
799 return TRUE;
800 }
801 if(!strcmp(scn_name,".rela.plt")) {
802 return TRUE;
803 }
804 if(!strcmp(scn_name,".rel.plt")) {
805 return TRUE;
806 }
807
808 if(!strcmp(scn_name,".data")) {
809 return TRUE;
810 }
811 if(!strcmp(scn_name,".rel.data")) {
812 return TRUE;
813 }
814 if(!strcmp(scn_name,".rela.data")) {
815 return TRUE;
816 }
817
818 if(!strcmp(scn_name,".got")) {
819 return TRUE;
820 }
821 if(!strcmp(scn_name,".rela.got")) {
822 return TRUE;
823 }
824 if(!strcmp(scn_name,".rel.got")) {
825 return TRUE;
826 }
827
828 return FALSE;
829 }
830 /* For an object file with an incorrect rela section name,
831 readelf prints correct debug information,
832 as the tool takes the section type instead
833 of the section name. So check the
834 section name but test section type. */
835 static int
is_a_relx_section(const char * scn_name,int type,int * is_rela)836 is_a_relx_section(const char *scn_name,int type,int *is_rela)
837 {
838 if(startswith(scn_name,".rela.")) {
839
840 *is_rela = TRUE;
841 return TRUE;
842 }
843 if(startswith(scn_name,".rel.")) {
844 *is_rela = FALSE;
845 return TRUE;
846 }
847 if (type == SHT_RELA) {
848 *is_rela = TRUE;
849 return TRUE;
850 }
851 if (type == SHT_REL) {
852 *is_rela = FALSE;
853 return TRUE;
854 }
855 *is_rela = FALSE;
856 return FALSE;
857 }
858
859 /* ASSERT: names like .debug_ or .zdebug_ never passed in here! */
860 static int
is_a_special_section_semi_dwarf(const char * scn_name)861 is_a_special_section_semi_dwarf(const char *scn_name)
862 {
863 if (!strcmp(scn_name,".strtab") ||
864 !strcmp(scn_name,".symtab")) {
865 return TRUE;
866 }
867 /* It's not one of these special sections referenced in
868 the test. */
869 return FALSE;
870 }
871
872 static int
this_section_dwarf_relevant(const char * scn_name,int type,int * is_rela)873 this_section_dwarf_relevant(const char *scn_name,int type, int *is_rela)
874 {
875 /* A small helper function for _dwarf_setup(). */
876 if (startswith(scn_name, ".zdebug_") ||
877 startswith(scn_name, ".debug_")) {
878 /* standard debug */
879 return TRUE;
880 }
881 if (_dwarf_ignorethissection(scn_name)) {
882 return FALSE;
883 }
884 /* Now check if a special section could be
885 in a section_group, but though seems unlikely. */
886 if (!strcmp(scn_name, ".eh_frame")) {
887 /* This is not really a group related file, but
888 it is harmless to consider it such. */
889 return TRUE;
890 }
891 if (!strcmp(scn_name, ".gnu_debuglink")) {
892 /* This is not a group or DWARF related file, but
893 it is useful for split dwarf. */
894 return TRUE;
895 }
896 if (!strcmp(scn_name, ".note.gnu.build-id")) {
897 /* This is not a group or DWARF related file, but
898 it is useful for split dwarf. */
899 return TRUE;
900 }
901 if(!strcmp(scn_name, ".gdb_index")) {
902 return TRUE;
903 }
904 if(is_a_special_section_semi_dwarf(scn_name)) {
905 return TRUE;
906 }
907 if(is_a_relx_section(scn_name,type,is_rela)) {
908 return TRUE;
909 }
910 /* All sorts of sections are of no interest: .text
911 .rel. and many others. */
912 return FALSE;
913 }
914
915 /* This assumes any non-Elf object files have no SHT_GROUP
916 sections. So this code will not be invoked on non-Elf objects.
917 One supposes this is unlikely to match any non-Elf
918 version of COMDAT. */
919 static int
insert_sht_list_in_group_map(Dwarf_Debug dbg,struct Dwarf_Obj_Access_Section_s * doas,unsigned comdat_group_number,unsigned section_number,Dwarf_Unsigned section_count,struct Dwarf_Obj_Access_Interface_s * obj,unsigned * did_add_map,Dwarf_Error * error)920 insert_sht_list_in_group_map(Dwarf_Debug dbg,
921 struct Dwarf_Obj_Access_Section_s *doas,
922 unsigned comdat_group_number,
923 unsigned section_number,
924 Dwarf_Unsigned section_count,
925 struct Dwarf_Obj_Access_Interface_s * obj,
926 unsigned *did_add_map,
927 Dwarf_Error *error)
928 {
929 struct Dwarf_Section_s secdata;
930 Dwarf_Small * data = 0;
931 int res = 0;
932 Dwarf_Small* secend = 0;
933
934 memset(&secdata,0,sizeof(secdata));
935 secdata.dss_size = doas->size;
936 secdata.dss_entrysize = doas->entrysize;
937 secdata.dss_group_number = 1; /* arbitrary. */
938 secdata.dss_index = section_number;
939 secdata.dss_name = ".group";
940 secdata.dss_standard_name = ".group";
941 secdata.dss_number = section_number;
942 secdata.dss_ignore_reloc_group_sec = TRUE;
943 res = _dwarf_load_section(dbg,&secdata,error);
944 if (res != DW_DLV_OK) {
945 if (secdata.dss_data_was_malloc) {
946 free(secdata.dss_data);
947 }
948 return res;
949 }
950 if (!secdata.dss_data) {
951 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
952 return DW_DLV_ERROR;
953 }
954 if (doas->entrysize != 4) {
955 if (secdata.dss_data_was_malloc) {
956 free(secdata.dss_data);
957 }
958 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
959 return DW_DLV_ERROR;
960 }
961 /* So now pick up the data in dss_data.
962 It is an array of 32 bit fields.
963 Entry zero is just a constant 1.
964 Each additional is a section number. */
965 data = secdata.dss_data;
966 secend = data + secdata.dss_size;
967 {
968 unsigned i = 1;
969 unsigned count = doas->size/doas->entrysize;
970 Dwarf_Unsigned fval = 0;
971
972 /* The fields treatments with regard
973 to endianness is unclear. In any case a single
974 bit should be on, as 0x01000000
975 without any endiannes swapping.
976 Or so it seems given limited evidence.
977 We read with length checking and allow the
978 reader to byte swap and then fix things.
979 At least one test case has big-endian
980 data but little-endian SHT_GROUP data. */
981 if ((data+DWARF_32BIT_SIZE) > secend) {
982 /* Duplicates the check in READ_UNALIGNED_CK
983 so we can free allocated memory bere. */
984 free(secdata.dss_data);
985 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
986 return DW_DLV_ERROR;
987 }
988 READ_UNALIGNED_CK(dbg,fval,Dwarf_Unsigned,
989 data,
990 DWARF_32BIT_SIZE,
991 error,
992 secend);
993 if (fval != 1 && fval != 0x1000000) {
994 /* Could be corrupted elf object. */
995 if (secdata.dss_data_was_malloc) {
996 free(secdata.dss_data);
997 }
998 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
999 return DW_DLV_ERROR;
1000 }
1001
1002 data = data + doas->entrysize;
1003 for (i = 1 ; i < count ; ++i) {
1004 Dwarf_Unsigned val = 0;
1005
1006 if ((data+DWARF_32BIT_SIZE) > secend) {
1007 /* Duplicates the check in READ_UNALIGNED_CK
1008 so we can free allocated memory bere. */
1009 if (secdata.dss_data_was_malloc) {
1010 free(secdata.dss_data);
1011 }
1012 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
1013 return DW_DLV_ERROR;
1014 }
1015 READ_UNALIGNED_CK(dbg,val,Dwarf_Unsigned,
1016 data,
1017 DWARF_32BIT_SIZE,
1018 error,
1019 secend);
1020 if (val > section_count) {
1021 /* Might be confused endianness by
1022 the compiler generating the SHT_GROUP.
1023 This is pretty horrible. */
1024 Dwarf_Unsigned valr = 0;
1025 _dwarf_memcpy_swap_bytes(&valr,&val,
1026 DWARF_32BIT_SIZE);
1027 if (valr > section_count) {
1028 if (secdata.dss_data_was_malloc) {
1029 free(secdata.dss_data);
1030 }
1031 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
1032 return DW_DLV_ERROR;
1033 }
1034 /* Ok. Yes, ugly. */
1035 val = valr;
1036 }
1037 {
1038 /* Ensure this group entry DWARF relevant before
1039 adding to group map */
1040 struct Dwarf_Obj_Access_Section_s doasx;
1041 int resx = DW_DLV_ERROR;
1042 int err = 0;
1043 int is_rela = FALSE;
1044
1045 memset(&doasx,0,sizeof(doasx));
1046 resx = obj->methods->get_section_info(obj->object,
1047 val,
1048 &doasx, &err);
1049 if (resx == DW_DLV_NO_ENTRY){
1050 /* Should we really ignore this? */
1051 continue;
1052 } else if (resx == DW_DLV_ERROR){
1053 if (secdata.dss_data_was_malloc) {
1054 free(secdata.dss_data);
1055 }
1056 _dwarf_error(dbg,error,err);
1057 return resx;
1058 }
1059 if (!this_section_dwarf_relevant(doasx.name,
1060 doasx.type,&is_rela) ) {
1061 continue;
1062 }
1063 data += DWARF_32BIT_SIZE;
1064 *did_add_map = TRUE;
1065 res = _dwarf_insert_in_group_map(dbg,
1066 comdat_group_number,val,
1067 doasx.name,
1068 error);
1069 if (res != DW_DLV_OK) {
1070 free(secdata.dss_data);
1071 return res;
1072 }
1073 }
1074 }
1075 }
1076 if (secdata.dss_data_was_malloc) {
1077 free(secdata.dss_data);
1078 }
1079 return DW_DLV_OK;
1080 }
1081
1082
1083
1084 /* Split dwarf CUs can be in an object with non-split
1085 or split may be in a separate object.
1086 If all in one object the default is to deal with group_number
1087 and ignore DW_GROUPNUMBER_DWO.
1088 If only .dwo the default is DW_GROUPNUMBER_DWO(2).
1089 Otherwise use DW_GROUP_NUMBER_BASE(1).
1090
1091 If there are COMDAT SHT_GROUP sections, these
1092 are assigned group numbers 3-N as needed.
1093
1094 At present this makes the assumption that COMDAT group
1095 (ie, SHT_GROUP) sections
1096 have lower section numbers than the sections COMDAT refers to.
1097 It is not clear whether this is guaranteed, COMDAT is not
1098 an official Elf thing and documentation is scarce.
1099 In the 1990's SGI folks and others formed a committee
1100 and attempted to get COMDAT and a feature allowing section
1101 numbers greater than 16 bits into Elf, but there was no
1102 group that was able to approve such things.
1103
1104 This is called once at dbg init time.
1105 */
1106
1107 static int
determine_target_group(Dwarf_Unsigned section_count,struct Dwarf_Obj_Access_Interface_s * obj,unsigned * group_number_out,Dwarf_Debug dbg,Dwarf_Error * error)1108 determine_target_group(Dwarf_Unsigned section_count,
1109 struct Dwarf_Obj_Access_Interface_s * obj,
1110 unsigned *group_number_out,
1111 Dwarf_Debug dbg,
1112 Dwarf_Error *error)
1113 {
1114 unsigned obj_section_index = 0;
1115 int found_group_one = 0;
1116 int found_group_two = 0;
1117 struct Dwarf_Group_Data_s *grp = 0;
1118 unsigned comdat_group_next = 3;
1119 unsigned lowest_comdat_groupnum = 0;
1120
1121 grp = &dbg->de_groupnumbers;
1122 grp->gd_number_of_groups = 0;
1123 grp->gd_number_of_sections = section_count;
1124 if (grp->gd_map) {
1125 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
1126 return DW_DLV_OK;
1127 }
1128 for (obj_section_index = 0; obj_section_index < section_count;
1129 ++obj_section_index) {
1130
1131 struct Dwarf_Obj_Access_Section_s doas;
1132 int res = DW_DLV_ERROR;
1133 int err = 0;
1134 const char *scn_name = 0;
1135 unsigned groupnumber = 0;
1136 unsigned mapgroupnumber = 0;
1137 int is_rela = FALSE;
1138
1139 memset(&doas,0,sizeof(doas));
1140 res = obj->methods->get_section_info(obj->object,
1141 obj_section_index,
1142 &doas, &err);
1143 if (res == DW_DLV_NO_ENTRY){
1144 return res;
1145 } else if (res == DW_DLV_ERROR){
1146 _dwarf_error(dbg, error,err);
1147 return res;
1148 }
1149
1150 if (doas.type == SHT_GROUP) {
1151 /* See assumptions in function comment above. */
1152 unsigned did_add_map = 0;
1153 /* Add to our map. Here we
1154 are assuming SHT_GROUP records come first.
1155 Till proven wrong. */
1156 res = insert_sht_list_in_group_map(dbg,&doas,
1157 comdat_group_next,
1158 obj_section_index,
1159 section_count,
1160 obj,
1161 &did_add_map,error);
1162 if (res != DW_DLV_OK) {
1163 return res;
1164 }
1165 if (!lowest_comdat_groupnum) {
1166 lowest_comdat_groupnum = comdat_group_next;
1167 }
1168 if (did_add_map) {
1169 ++grp->gd_number_of_groups;
1170 ++comdat_group_next;
1171 }
1172 continue;
1173 }
1174 scn_name = doas.name;
1175 if (!this_section_dwarf_relevant(scn_name,doas.type,&is_rela) ) {
1176 continue;
1177 }
1178
1179 /* Now at a 'normal' section, though we do not
1180 quite know what group it is. */
1181
1182 res = _dwarf_section_get_target_group_from_map(dbg,
1183 obj_section_index,&groupnumber,error);
1184 if (res == DW_DLV_OK ) {
1185 /* groupnumber is set. Fall through.
1186 All COMDAT group should get here. */
1187 mapgroupnumber = groupnumber;
1188 } else if (res == DW_DLV_ERROR) {
1189 return res;
1190 } else { /* DW_DLV_NO_ENTRY */
1191 /* Normal non-COMDAT. groupnumber is zero. */
1192 }
1193
1194
1195 /* BUILDING_MAP. See also BUILDING_SECTIONS, SETUP_SECTION */
1196 if (!groupnumber) {
1197 res =_dwarf_dwo_groupnumber_given_name(scn_name,
1198 &groupnumber);
1199 /* DW_DLV_ERROR impossible here. */
1200 if (res == DW_DLV_OK) {
1201 /* groupnumber set 2 */
1202 } else {
1203 /* This is what it has to be.
1204 .rela in here too. */
1205 groupnumber = DW_GROUPNUMBER_BASE;
1206 }
1207 }
1208 if (is_a_relx_section(scn_name,doas.type,&is_rela)) {
1209 continue;
1210 }
1211
1212 /* ASSERT: groupnumber non-zero now */
1213 if (!is_a_special_section_semi_dwarf(scn_name)) {
1214 if (mapgroupnumber) {
1215 /* Already in group map */
1216 continue;
1217 }
1218 /* !mapgroupnumber */
1219 res = _dwarf_insert_in_group_map(dbg,
1220 groupnumber,obj_section_index,
1221 scn_name,
1222 error);
1223 if (res != DW_DLV_OK) {
1224 return res;
1225 }
1226 if (groupnumber == 1) {
1227 found_group_one++;
1228 } else if (groupnumber == 2) {
1229 found_group_two++;
1230 }
1231 continue;
1232 }
1233 }
1234 if (found_group_two) {
1235 ++grp->gd_number_of_groups;
1236 }
1237 if (found_group_one) {
1238 *group_number_out = DW_GROUPNUMBER_BASE;
1239 ++grp->gd_number_of_groups;
1240 } else {
1241 if (found_group_two) {
1242 *group_number_out = DW_GROUPNUMBER_DWO;
1243 } else {
1244 if (lowest_comdat_groupnum) {
1245 *group_number_out = lowest_comdat_groupnum;
1246 } else {
1247 *group_number_out = DW_GROUPNUMBER_BASE;
1248 }
1249 }
1250 }
1251 return DW_DLV_OK;
1252 }
1253
1254
1255
1256 static int
_dwarf_setup(Dwarf_Debug dbg,Dwarf_Error * error)1257 _dwarf_setup(Dwarf_Debug dbg, Dwarf_Error * error)
1258 {
1259 const char *scn_name = 0;
1260 struct Dwarf_Obj_Access_Interface_s * obj = 0;
1261 int resn = 0;
1262 struct Dwarf_Section_s **sections = 0;
1263 Dwarf_Endianness endianness;
1264 Dwarf_Unsigned section_count = 0;
1265 unsigned default_group_number = 0;
1266 unsigned foundDwarf = FALSE;
1267 unsigned obj_section_index = 0;
1268
1269 dbg->de_assume_string_in_bounds =
1270 _dwarf_assume_string_in_bounds;
1271 /* First make an arbitrary assumption. */
1272 dbg->de_same_endian = 1;
1273 dbg->de_copy_word = _dwarf_memcpy_noswap_bytes;
1274 obj = dbg->de_obj_file;
1275 endianness = obj->methods->get_byte_order(obj->object);
1276 /* Then adjust any changes we need. */
1277 #ifdef WORDS_BIGENDIAN
1278 dbg->de_big_endian_object = 1;
1279 if (endianness == DW_OBJECT_LSB ) {
1280 dbg->de_same_endian = 0;
1281 dbg->de_big_endian_object = 0;
1282 dbg->de_copy_word = _dwarf_memcpy_swap_bytes;
1283 }
1284 #else /* little endian */
1285 dbg->de_big_endian_object = 0;
1286 if (endianness == DW_OBJECT_MSB ) {
1287 dbg->de_same_endian = 0;
1288 dbg->de_big_endian_object = 1;
1289 dbg->de_copy_word = _dwarf_memcpy_swap_bytes;
1290 }
1291 #endif /* !WORDS_BIGENDIAN */
1292
1293
1294 /* The following de_length_size is Not Too Significant. Only used
1295 one calculation, and an approximate one at that. */
1296 dbg->de_length_size = obj->methods->get_length_size(obj->object);
1297 dbg->de_pointer_size = obj->methods->get_pointer_size(obj->object);
1298
1299 section_count = obj->methods->get_section_count(obj->object);
1300 resn = determine_target_group(section_count,obj,
1301 &default_group_number,dbg,error);
1302 if (resn == DW_DLV_ERROR) {
1303 return DW_DLV_ERROR;
1304 }
1305 if (dbg->de_groupnumber == DW_GROUPNUMBER_ANY) {
1306 dbg->de_groupnumber = default_group_number;
1307 }
1308 /* Allocate space to record references to debug sections, that can
1309 be referenced by RELA sections in the 'sh_info' field. */
1310 sections = (struct Dwarf_Section_s **)calloc(section_count + 1,
1311 sizeof(struct Dwarf_Section_s *));
1312 if (!sections) {
1313 /* Impossible case, we hope. Give up. */
1314 _dwarf_error(dbg, error, DW_DLE_SECTION_ERROR);
1315 return DW_DLV_ERROR;
1316 }
1317
1318 /* We can skip index 0 when considering ELF files, but not other
1319 object types. Indeed regardless of the object type we should
1320 skip section 0 here.
1321 This is a convention. We depend on it.
1322 Non-elf object access code should
1323 (in itself) understand we will index beginning at 1 and adjust
1324 itself to deal with this Elf convention. Without this
1325 convention various parts of the code in this file won't work correctly.
1326 A dss_index of 0 must not be used, even though we start at 0
1327 here. So the get_section_info() must adapt to the situation
1328 (the elf version does automatically as a result of Elf having
1329 a section zero with zero length and an empty name). */
1330
1331 /* ASSERT: all group map entries set up. */
1332
1333 for (obj_section_index = 0; obj_section_index < section_count;
1334 ++obj_section_index) {
1335
1336 struct Dwarf_Obj_Access_Section_s doas;
1337 int res = DW_DLV_ERROR;
1338 int err = 0;
1339 unsigned groupnumber = 0;
1340 unsigned mapgroupnumber = 0;
1341 int is_rela = FALSE;
1342
1343 res = _dwarf_section_get_target_group_from_map(dbg,obj_section_index,
1344 &groupnumber,error);
1345 if (res == DW_DLV_OK ) {
1346 /* groupnumber is set. Fall through */
1347 mapgroupnumber = groupnumber;
1348 } else if (res == DW_DLV_ERROR) {
1349 free(sections);
1350 return res;
1351 } else { /* DW_DLV_NO_ENTRY */
1352 /* fall through, a BASE or DWO group, possibly */
1353 }
1354 memset(&doas,0,sizeof(doas));
1355 res = obj->methods->get_section_info(obj->object,
1356 obj_section_index,
1357 &doas, &err);
1358 if (res == DW_DLV_NO_ENTRY){
1359 free(sections);
1360 return res;
1361 } else if (res == DW_DLV_ERROR){
1362 free(sections);
1363 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
1364 }
1365 scn_name = doas.name;
1366 if (!groupnumber) {
1367 /* This finds dwo sections, group 2 */
1368 res = _dwarf_dwo_groupnumber_given_name(scn_name,
1369 &groupnumber);
1370 if (res == DW_DLV_NO_ENTRY) {
1371 /* No, must be group 1 */
1372 groupnumber = DW_GROUPNUMBER_BASE;
1373 }
1374 }
1375 if (!this_section_dwarf_relevant(scn_name,doas.type,&is_rela) ) {
1376 continue;
1377 }
1378 if (!is_a_relx_section(scn_name,doas.type,&is_rela)
1379 && !is_a_special_section_semi_dwarf(scn_name)) {
1380 /* We do these actions only for group-related
1381 sections. Do for .debug_info etc,
1382 never for .strtab or .rela.*
1383 We already tested for relevance, so that part
1384 is not news. */
1385 if(mapgroupnumber == dbg->de_groupnumber) {
1386 /* OK. Mapped. Part of the group.. This will
1387 catch the cases where there are versions of
1388 a section in multiple COMDATs and in BASE
1389 an DWO to get the right one */
1390 } else {
1391 /* This section not mapped into this group. */
1392 if (groupnumber == 1 && dbg->de_groupnumber > 2 &&
1393 !_dwarf_section_in_group_by_name(dbg,scn_name,
1394 dbg->de_groupnumber)) {
1395 /* Load the section (but as group 1) */
1396 } else {
1397 continue;
1398 }
1399 }
1400 }
1401 /* BUILDING_SECTIONS. See also BUILDING_MAP, SETUP_SECTION */
1402 {
1403 /* Build up the sections table and the
1404 de_debug* etc pointers in Dwarf_Debug. */
1405 struct Dwarf_dbg_sect_s *section;
1406
1407 int found_match = FALSE;
1408
1409 res = is_section_name_known_already(dbg,scn_name);
1410 if (res == DW_DLV_OK) {
1411 /* DUPLICATE */
1412 free(sections);
1413 DWARF_DBG_ERROR(dbg, DW_DLE_SECTION_DUPLICATION,
1414 DW_DLV_ERROR);
1415 } else if (res == DW_DLV_ERROR) {
1416 free(sections);
1417 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
1418 }
1419 /* No entry: new-to-us section, the normal case. */
1420 res = enter_section_in_de_debug_sections_array(dbg,scn_name,
1421 obj_section_index, groupnumber,&err);
1422 if (res == DW_DLV_OK) {
1423 section = &dbg->de_debug_sections[
1424 dbg->de_debug_sections_total_entries-1];
1425 res = get_basic_section_data(dbg,
1426 section->ds_secdata, &doas,
1427 obj_section_index,
1428 groupnumber,
1429 error,
1430 section->ds_duperr,
1431 section->ds_emptyerr);
1432 if (res != DW_DLV_OK) {
1433 free(sections);
1434 return res;
1435 }
1436 sections[obj_section_index] = section->ds_secdata;
1437 foundDwarf += section->ds_have_dwarf;
1438 found_match = TRUE;
1439 /* Normal section set up.
1440 Fall through. */
1441 } else if (res == DW_DLV_NO_ENTRY) {
1442 /* We get here for relocation sections.
1443 Fall through. */
1444 } else {
1445 free(sections);
1446 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
1447 }
1448
1449 if (!found_match) {
1450 /* For an object file with incorrect rel[a] section name,
1451 the 'readelf' tool, prints correct debug information,
1452 as the tool takes the section type instead
1453 of the section name. If the current section
1454 is a RELA one and the 'sh_info'
1455 refers to a debug section, add the relocation data. */
1456 if (is_a_relx_section(scn_name,doas.type,&is_rela)) {
1457 if ( doas.info < section_count) {
1458 if (sections[doas.info]) {
1459 add_relx_data_to_secdata(sections[doas.info],
1460 &doas,
1461 obj_section_index,is_rela);
1462 }
1463 } else {
1464 /* Something is wrong with the ELF file. */
1465 free(sections);
1466 DWARF_DBG_ERROR(dbg, DW_DLE_ELF_SECT_ERR, DW_DLV_ERROR);
1467 }
1468 }
1469 }
1470 /* Fetch next section */
1471 }
1472 }
1473
1474 /* Free table with section information. */
1475 free(sections);
1476 if (foundDwarf) {
1477 return DW_DLV_OK;
1478 }
1479 return DW_DLV_NO_ENTRY;
1480 }
1481
1482 /* There is one table per CU and one per TU, and each
1483 table refers to the associated other DWARF data
1484 for that CU or TU.
1485 See DW_SECT_*
1486
1487 In DWARF4 the type units are in .debug_types
1488 In DWARF5 the type units are in .debug_info.
1489 */
1490
1491 static int
load_debugfission_tables(Dwarf_Debug dbg,Dwarf_Error * error)1492 load_debugfission_tables(Dwarf_Debug dbg,Dwarf_Error *error)
1493 {
1494 int i = 0;
1495 if (dbg->de_debug_cu_index.dss_size ==0 &&
1496 dbg->de_debug_tu_index.dss_size ==0) {
1497 /* This is the normal case.
1498 No debug fission. Not a .dwp object. */
1499 return DW_DLV_NO_ENTRY;
1500 }
1501
1502 for (i = 0; i < 2; ++i) {
1503 Dwarf_Xu_Index_Header xuptr = 0;
1504 struct Dwarf_Section_s* dwsect = 0;
1505 Dwarf_Unsigned version = 0;
1506 Dwarf_Unsigned number_of_cols /* L */ = 0;
1507 Dwarf_Unsigned number_of_CUs /* N */ = 0;
1508 Dwarf_Unsigned number_of_slots /* M */ = 0;
1509 const char *secname = 0;
1510 int res = 0;
1511 const char *type = 0;
1512
1513 if (i == 0) {
1514 dwsect = &dbg->de_debug_cu_index;
1515 type = "cu";
1516 } else {
1517 dwsect = &dbg->de_debug_tu_index;
1518 type = "tu";
1519 }
1520 if ( !dwsect->dss_size ) {
1521 continue;
1522 }
1523 res = dwarf_get_xu_index_header(dbg,type,
1524 &xuptr,&version,&number_of_cols,
1525 &number_of_CUs,&number_of_slots,
1526 &secname,error);
1527 if (res == DW_DLV_NO_ENTRY) {
1528 continue;
1529 }
1530 if (res != DW_DLV_OK) {
1531 return res;
1532 }
1533 if (i == 0) {
1534 dbg->de_cu_hashindex_data = xuptr;
1535 } else {
1536 dbg->de_tu_hashindex_data = xuptr;
1537 }
1538 }
1539 return DW_DLV_OK;
1540 }
1541
1542 /*
1543 Use a Dwarf_Obj_Access_Interface to kick things off. All other
1544 init routines eventually use this one.
1545 The returned Dwarf_Debug contains a copy of *obj
1546 the callers copy of *obj may be freed whenever the caller
1547 wishes.
1548 */
1549 int
dwarf_object_init(Dwarf_Obj_Access_Interface * obj,Dwarf_Handler errhand,Dwarf_Ptr errarg,Dwarf_Debug * ret_dbg,Dwarf_Error * error)1550 dwarf_object_init(Dwarf_Obj_Access_Interface* obj,
1551 Dwarf_Handler errhand,
1552 Dwarf_Ptr errarg, Dwarf_Debug* ret_dbg,
1553 Dwarf_Error* error)
1554 {
1555 return dwarf_object_init_b(obj,errhand,errarg,
1556 DW_GROUPNUMBER_ANY,ret_dbg,error);
1557 }
1558
1559 /* New March 2017. Enables dealing with DWARF5 split dwarf more fully. */
1560 int
dwarf_object_init_b(Dwarf_Obj_Access_Interface * obj,Dwarf_Handler errhand,Dwarf_Ptr errarg,unsigned groupnumber,Dwarf_Debug * ret_dbg,Dwarf_Error * error)1561 dwarf_object_init_b(Dwarf_Obj_Access_Interface* obj, Dwarf_Handler errhand,
1562 Dwarf_Ptr errarg,
1563 unsigned groupnumber,
1564 Dwarf_Debug* ret_dbg,
1565 Dwarf_Error* error)
1566 {
1567 Dwarf_Debug dbg = 0;
1568 int setup_result = DW_DLV_OK;
1569
1570 dbg = _dwarf_get_debug();
1571 if (dbg == NULL) {
1572 DWARF_DBG_ERROR(dbg, DW_DLE_DBG_ALLOC, DW_DLV_ERROR);
1573 }
1574 dbg->de_errhand = errhand;
1575 dbg->de_errarg = errarg;
1576 dbg->de_frame_rule_initial_value = DW_FRAME_REG_INITIAL_VALUE;
1577 dbg->de_frame_reg_rules_entry_count = DW_FRAME_LAST_REG_NUM;
1578 #ifdef HAVE_OLD_FRAME_CFA_COL
1579 /* DW_FRAME_CFA_COL is really only suitable for old libdwarf frame
1580 interfaces and its value of 0 there is only usable where
1581 (as in MIPS) register 0 has no value other than 0 so
1582 we can use the frame table column 0 for the CFA value
1583 (and rely on client software to know when 'register 0'
1584 is the cfa and when to just use a value 0 for register 0).
1585 */
1586 dbg->de_frame_cfa_col_number = DW_FRAME_CFA_COL;
1587 #else
1588 dbg->de_frame_cfa_col_number = DW_FRAME_CFA_COL3;
1589 #endif
1590 dbg->de_frame_same_value_number = DW_FRAME_SAME_VAL;
1591 dbg->de_frame_undefined_value_number = DW_FRAME_UNDEFINED_VAL;
1592
1593 dbg->de_obj_file = obj;
1594 dbg->de_groupnumber = groupnumber;
1595 setup_result = _dwarf_setup(dbg, error);
1596 if (setup_result == DW_DLV_OK) {
1597 int fission_result = load_debugfission_tables(dbg,error);
1598 /* In most cases we get
1599 setup_result == DW_DLV_NO_ENTRY here
1600 as having debugfission (.dwp objects)
1601 is fairly rare. */
1602 if (fission_result == DW_DLV_ERROR) {
1603 /* Something is very wrong. */
1604 setup_result = fission_result;
1605 }
1606 }
1607 if (setup_result != DW_DLV_OK) {
1608 int freeresult = 0;
1609 int myerr = 0;
1610 dwarfstring msg;
1611
1612 dwarfstring_constructor(&msg);
1613 /* We cannot use any _dwarf_setup()
1614 error here as
1615 we are freeing dbg, making that error (setup
1616 as part of dbg) stale.
1617 Hence we have to make a new error without a dbg.
1618 But error might be NULL and the init call
1619 error-handler function might be set.
1620 */
1621 if ( (setup_result == DW_DLV_ERROR) && *error ) {
1622 /* Preserve our _dwarf_setup error number, but
1623 this does not apply if error NULL. */
1624 myerr = dwarf_errno(*error);
1625 dwarfstring_append(&msg,dwarf_errmsg(*error));
1626 /* deallocate the soon-stale error pointer. */
1627 dwarf_dealloc(dbg,*error,DW_DLA_ERROR);
1628 *error = 0;
1629 }
1630 /* The status we want to return here is of _dwarf_setup,
1631 not of the _dwarf_free_all_of_one_debug(dbg) call.
1632 So use a local status variable for the free. */
1633 freeresult = _dwarf_free_all_of_one_debug(dbg);
1634 dbg = 0;
1635 /* DW_DLV_NO_ENTRY not possible in freeresult */
1636 if (freeresult == DW_DLV_ERROR) {
1637 /* Use the _dwarf_setup error number.
1638 If error is NULL the following will issue
1639 a message on stderr and abort(), as without
1640 dbg there is no error-handler function.
1641 */
1642 _dwarf_error_string(dbg,error,DW_DLE_DBG_ALLOC,
1643 dwarfstring_string(&msg));
1644 dwarfstring_destructor(&msg);
1645 return DW_DLV_ERROR;
1646 }
1647 if (setup_result == DW_DLV_ERROR) {
1648 /* Use the _dwarf_setup error number.
1649 If error is NULL the following will issue
1650 a message on stderr and abort(), as without
1651 dbg there is no error-handler function.
1652 */
1653 _dwarf_error_string(dbg,error,myerr,
1654 dwarfstring_string(&msg));
1655 }
1656 dwarfstring_destructor(&msg);
1657 return setup_result;
1658 }
1659 dwarf_harmless_init(&dbg->de_harmless_errors,
1660 DW_HARMLESS_ERROR_CIRCULAR_LIST_DEFAULT_SIZE);
1661 *ret_dbg = dbg;
1662 return DW_DLV_OK;
1663 }
1664
1665 /* A finish routine that is completely unaware of ELF.
1666
1667 Frees all memory that was not previously freed by
1668 dwarf_dealloc.
1669 Aside from certain categories. */
1670 int
dwarf_object_finish(Dwarf_Debug dbg,Dwarf_Error * error)1671 dwarf_object_finish(Dwarf_Debug dbg, Dwarf_Error * error)
1672 {
1673 int res = 0;
1674
1675 res = _dwarf_free_all_of_one_debug(dbg);
1676 if (res == DW_DLV_ERROR) {
1677 DWARF_DBG_ERROR(dbg, DW_DLE_DBG_ALLOC, DW_DLV_ERROR);
1678 }
1679 return res;
1680 }
1681
1682 #ifdef HAVE_ZLIB
1683 /* case 1:
1684 The input stream is assumed to contain
1685 the four letters
1686 ZLIB
1687 Followed by 8 bytes of the size of the
1688 uncompressed stream. Presented as
1689 a big-endian binary number.
1690 Following that is the stream to decompress.
1691
1692 case 2:
1693 The section flag bit SHF_COMPRESSED (1 << 11)
1694 must be set.
1695 we then do the eqivalent of reading a
1696 Elf32_External_Chdr
1697 or
1698 Elf64_External_Chdr
1699 to get the type (which must be 1)
1700 and the decompressed_length.
1701 Then what follows the implicit Chdr is decompressed.
1702 */
1703
1704 /* ALLOWED_ZLIB_INFLATION is a heuristic, not necessarily right.
1705 The test case klingler2/compresseddebug.amd64 actually
1706 inflates about 8 times. */
1707 #define ALLOWED_ZLIB_INFLATION 16
1708 static int
do_decompress_zlib(Dwarf_Debug dbg,struct Dwarf_Section_s * section,Dwarf_Error * error)1709 do_decompress_zlib(Dwarf_Debug dbg,
1710 struct Dwarf_Section_s *section,
1711 Dwarf_Error * error)
1712 {
1713 Bytef *basesrc = (Bytef *)section->dss_data;
1714 Bytef *src = (Bytef *)basesrc;
1715 uLong srclen = section->dss_size;
1716 Dwarf_Unsigned flags = section->dss_flags;
1717 Dwarf_Small *endsection = 0;
1718 int res = 0;
1719 Bytef *dest = 0;
1720 uLongf destlen = 0;
1721 Dwarf_Unsigned uncompressed_len = 0;
1722
1723 endsection = basesrc + srclen;
1724 if ((src + 12) >endsection) {
1725 DWARF_DBG_ERROR(dbg, DW_DLE_ZLIB_SECTION_SHORT, DW_DLV_ERROR);
1726 }
1727 section->dss_compressed_length = section->dss_size;
1728 if(!strncmp("ZLIB",(const char *)src,4)) {
1729 unsigned i = 0;
1730 unsigned l = 8;
1731 unsigned char *c = src+4;
1732 for( ; i < l; ++i,c++) {
1733 uncompressed_len <<= 8;
1734 uncompressed_len += *c;
1735 }
1736 src = src + 12;
1737 srclen -= 12;
1738 section->dss_uncompressed_length = uncompressed_len;
1739 section->dss_ZLIB_compressed = TRUE;
1740 } else if (flags & SHF_COMPRESSED) {
1741 /* The prefix is a struct:
1742 unsigned int type; followed by pad if following are 64bit!
1743 size-of-target-address size
1744 size-of-target-address
1745 */
1746 Dwarf_Small *ptr = (Dwarf_Small *)src;
1747 Dwarf_Unsigned type = 0;
1748 Dwarf_Unsigned size = 0;
1749 /* Dwarf_Unsigned addralign = 0; */
1750 unsigned fldsize = dbg->de_pointer_size;
1751 unsigned structsize = 3* fldsize;
1752
1753 READ_UNALIGNED_CK(dbg,type,Dwarf_Unsigned,ptr,
1754 DWARF_32BIT_SIZE,
1755 error,endsection);
1756 ptr += fldsize;
1757 READ_UNALIGNED_CK(dbg,size,Dwarf_Unsigned,ptr,fldsize,
1758 error,endsection);
1759 if (type != ELFCOMPRESS_ZLIB) {
1760 DWARF_DBG_ERROR(dbg, DW_DLE_ZDEBUG_INPUT_FORMAT_ODD,
1761 DW_DLV_ERROR);
1762 }
1763 uncompressed_len = size;
1764 section->dss_uncompressed_length = uncompressed_len;
1765 src += structsize;
1766 srclen -= structsize;
1767 section->dss_shf_compressed = TRUE;
1768 } else {
1769 DWARF_DBG_ERROR(dbg, DW_DLE_ZDEBUG_INPUT_FORMAT_ODD,
1770 DW_DLV_ERROR);
1771 }
1772 {
1773 /* According to zlib.net zlib essentially never expands
1774 the data when compressing. There is no statement
1775 about any effective limit in the compression factor
1776 though we, here, assume such a limit to check
1777 for sanity in the object file.
1778 These tests are heuristics. */
1779 Dwarf_Unsigned max_inflated_len = srclen*ALLOWED_ZLIB_INFLATION;
1780
1781 if (srclen > 50) {
1782 /* If srclen not super tiny lets check the following. */
1783 if (uncompressed_len < (srclen/2)) {
1784 /* Violates the approximate invariant about
1785 compression not actually inflating. */
1786 DWARF_DBG_ERROR(dbg, DW_DLE_ZLIB_UNCOMPRESS_ERROR,
1787 DW_DLV_ERROR);
1788 }
1789 }
1790 if (max_inflated_len < srclen) {
1791 /* The calculation overflowed. */
1792 DWARF_DBG_ERROR(dbg, DW_DLE_ZLIB_UNCOMPRESS_ERROR, DW_DLV_ERROR);
1793 }
1794 if (uncompressed_len > max_inflated_len) {
1795 DWARF_DBG_ERROR(dbg, DW_DLE_ZLIB_UNCOMPRESS_ERROR, DW_DLV_ERROR);
1796 }
1797 }
1798 if( (src +srclen) > endsection) {
1799 DWARF_DBG_ERROR(dbg, DW_DLE_ZLIB_SECTION_SHORT, DW_DLV_ERROR);
1800 }
1801 destlen = uncompressed_len;
1802 dest = malloc(destlen);
1803 if(!dest) {
1804 DWARF_DBG_ERROR(dbg, DW_DLE_ALLOC_FAIL, DW_DLV_ERROR);
1805 }
1806 res = uncompress(dest,&destlen,src,srclen);
1807 if (res == Z_BUF_ERROR) {
1808 free(dest);
1809 DWARF_DBG_ERROR(dbg, DW_DLE_ZLIB_BUF_ERROR, DW_DLV_ERROR);
1810 } else if (res == Z_MEM_ERROR) {
1811 free(dest);
1812 DWARF_DBG_ERROR(dbg, DW_DLE_ALLOC_FAIL, DW_DLV_ERROR);
1813 } else if (res != Z_OK) {
1814 free(dest);
1815 /* Probably Z_DATA_ERROR. */
1816 DWARF_DBG_ERROR(dbg, DW_DLE_ZLIB_DATA_ERROR, DW_DLV_ERROR);
1817 }
1818 /* Z_OK */
1819 section->dss_data = dest;
1820 section->dss_size = destlen;
1821 section->dss_data_was_malloc = TRUE;
1822 section->dss_did_decompress = TRUE;
1823 return DW_DLV_OK;
1824 }
1825 #endif /* HAVE_ZLIB */
1826
1827
1828 /* Load the ELF section with the specified index and set its
1829 dss_data pointer to the memory where it was loaded. */
1830 int
_dwarf_load_section(Dwarf_Debug dbg,struct Dwarf_Section_s * section,Dwarf_Error * error)1831 _dwarf_load_section(Dwarf_Debug dbg,
1832 struct Dwarf_Section_s *section,
1833 Dwarf_Error * error)
1834 {
1835 int res = DW_DLV_ERROR;
1836 int err = 0;
1837 struct Dwarf_Obj_Access_Interface_s *o = 0;
1838
1839 /* check to see if the section is already loaded */
1840 if (section->dss_data != NULL) {
1841 return DW_DLV_OK;
1842 }
1843 o = dbg->de_obj_file;
1844 /* There is an elf convention that section index 0
1845 is reserved, and that section is always empty.
1846 Non-elf object formats must honor
1847 that by ensuring that (when they
1848 assign numbers to 'sections' or
1849 'section-like-things') they never
1850 assign a real section section-number
1851 0 to dss_index.
1852
1853 There is also a convention for 'bss' that that section
1854 and its like sections have no data but do have a size.
1855 That is never true of DWARF sections */
1856 res = o->methods->load_section(
1857 o->object, section->dss_index,
1858 §ion->dss_data, &err);
1859 if (res == DW_DLV_ERROR) {
1860 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
1861 }
1862 /* For PE and mach-o all section data was always
1863 malloc'd. We do not need to set dss_data_was_malloc
1864 though as the o->object data will eventually free
1865 the original section data.
1866 The first character of any o->object struct gives the type. */
1867
1868 if (res == DW_DLV_NO_ENTRY) {
1869 /* Gets this for section->dss_index 0.
1870 Which by ELF definition is a section index
1871 which is not used (reserved by Elf to
1872 mean no-section-index).
1873 Otherwise NULL dss_data gets error.
1874 BSS would legitimately have no data, but
1875 no DWARF related section could possbly be bss. */
1876 return res;
1877 }
1878 if (section->dss_ignore_reloc_group_sec) {
1879 /* Neither zdebug nor reloc apply to .group sections. */
1880 return res;
1881 }
1882 if ((section->dss_zdebug_requires_decompress ||
1883 section->dss_shf_compressed ||
1884 section->dss_ZLIB_compressed) &&
1885 !section->dss_did_decompress) {
1886 if (!section->dss_data) {
1887 /* Impossible. This makes no sense.
1888 Corrupt object. */
1889 DWARF_DBG_ERROR(dbg, DW_DLE_COMPRESSED_EMPTY_SECTION, DW_DLV_ERROR);
1890 }
1891 #ifdef HAVE_ZLIB
1892 res = do_decompress_zlib(dbg,section,error);
1893 if (res != DW_DLV_OK) {
1894 return res;
1895 }
1896 section->dss_did_decompress = TRUE;
1897 #else
1898 DWARF_DBG_ERROR(dbg,DW_DLE_ZDEBUG_REQUIRES_ZLIB, DW_DLV_ERROR);
1899 #endif
1900 }
1901 if (_dwarf_apply_relocs == 0) {
1902 return res;
1903 }
1904 if (section->dss_reloc_size == 0) {
1905 return res;
1906 }
1907 if (!o->methods->relocate_a_section) {
1908 return res;
1909 }
1910 /*apply relocations */
1911 res = o->methods->relocate_a_section( o->object,
1912 section->dss_index, dbg, &err);
1913 if (res == DW_DLV_ERROR) {
1914 DWARF_DBG_ERROR(dbg, err, res);
1915 }
1916 return res;
1917 }
1918
1919 /* This is a hack so clients can verify offsets.
1920 Added April 2005 so that debugger can detect broken offsets
1921 (which happened in an IRIX -64 executable larger than 2GB
1922 using MIPSpro 7.3.1.3 compilers. A couple .debug_pubnames
1923 offsets were wrong.).
1924 */
1925 int
dwarf_get_section_max_offsets(Dwarf_Debug dbg,Dwarf_Unsigned * debug_info_size,Dwarf_Unsigned * debug_abbrev_size,Dwarf_Unsigned * debug_line_size,Dwarf_Unsigned * debug_loc_size,Dwarf_Unsigned * debug_aranges_size,Dwarf_Unsigned * debug_macinfo_size,Dwarf_Unsigned * debug_pubnames_size,Dwarf_Unsigned * debug_str_size,Dwarf_Unsigned * debug_frame_size,Dwarf_Unsigned * debug_ranges_size,Dwarf_Unsigned * debug_typenames_size)1926 dwarf_get_section_max_offsets(Dwarf_Debug dbg,
1927 Dwarf_Unsigned * debug_info_size,
1928 Dwarf_Unsigned * debug_abbrev_size,
1929 Dwarf_Unsigned * debug_line_size,
1930 Dwarf_Unsigned * debug_loc_size,
1931 Dwarf_Unsigned * debug_aranges_size,
1932 Dwarf_Unsigned * debug_macinfo_size,
1933 Dwarf_Unsigned * debug_pubnames_size,
1934 Dwarf_Unsigned * debug_str_size,
1935 Dwarf_Unsigned * debug_frame_size,
1936 Dwarf_Unsigned * debug_ranges_size,
1937 Dwarf_Unsigned * debug_typenames_size)
1938 {
1939 *debug_info_size = dbg->de_debug_info.dss_size;
1940 *debug_abbrev_size = dbg->de_debug_abbrev.dss_size;
1941 *debug_line_size = dbg->de_debug_line.dss_size;
1942 *debug_loc_size = dbg->de_debug_loc.dss_size;
1943 *debug_aranges_size = dbg->de_debug_aranges.dss_size;
1944 *debug_macinfo_size = dbg->de_debug_macinfo.dss_size;
1945 *debug_pubnames_size = dbg->de_debug_pubnames.dss_size;
1946 *debug_str_size = dbg->de_debug_str.dss_size;
1947 *debug_frame_size = dbg->de_debug_frame.dss_size;
1948 *debug_ranges_size = dbg->de_debug_ranges.dss_size;
1949 *debug_typenames_size = dbg->de_debug_typenames.dss_size;
1950 return DW_DLV_OK;
1951 }
1952 /* This adds the new types size (new section) to the output data.
1953 Oct 27, 2011. */
1954 int
dwarf_get_section_max_offsets_b(Dwarf_Debug dbg,Dwarf_Unsigned * debug_info_size,Dwarf_Unsigned * debug_abbrev_size,Dwarf_Unsigned * debug_line_size,Dwarf_Unsigned * debug_loc_size,Dwarf_Unsigned * debug_aranges_size,Dwarf_Unsigned * debug_macinfo_size,Dwarf_Unsigned * debug_pubnames_size,Dwarf_Unsigned * debug_str_size,Dwarf_Unsigned * debug_frame_size,Dwarf_Unsigned * debug_ranges_size,Dwarf_Unsigned * debug_typenames_size,Dwarf_Unsigned * debug_types_size)1955 dwarf_get_section_max_offsets_b(Dwarf_Debug dbg,
1956 Dwarf_Unsigned * debug_info_size,
1957 Dwarf_Unsigned * debug_abbrev_size,
1958 Dwarf_Unsigned * debug_line_size,
1959 Dwarf_Unsigned * debug_loc_size,
1960 Dwarf_Unsigned * debug_aranges_size,
1961 Dwarf_Unsigned * debug_macinfo_size,
1962 Dwarf_Unsigned * debug_pubnames_size,
1963 Dwarf_Unsigned * debug_str_size,
1964 Dwarf_Unsigned * debug_frame_size,
1965 Dwarf_Unsigned * debug_ranges_size,
1966 Dwarf_Unsigned * debug_typenames_size,
1967 Dwarf_Unsigned * debug_types_size)
1968 {
1969 *debug_info_size = dbg->de_debug_info.dss_size;
1970 *debug_abbrev_size = dbg->de_debug_abbrev.dss_size;
1971 *debug_line_size = dbg->de_debug_line.dss_size;
1972 *debug_loc_size = dbg->de_debug_loc.dss_size;
1973 *debug_aranges_size = dbg->de_debug_aranges.dss_size;
1974 *debug_macinfo_size = dbg->de_debug_macinfo.dss_size;
1975 *debug_pubnames_size = dbg->de_debug_pubnames.dss_size;
1976 *debug_str_size = dbg->de_debug_str.dss_size;
1977 *debug_frame_size = dbg->de_debug_frame.dss_size;
1978 *debug_ranges_size = dbg->de_debug_ranges.dss_size;
1979 *debug_typenames_size = dbg->de_debug_typenames.dss_size;
1980 *debug_types_size = dbg->de_debug_types.dss_size;
1981 return DW_DLV_OK;
1982 }
1983
1984 /* Now with sections new to DWARF5 (unofficial list,preliminary) */
1985 int
dwarf_get_section_max_offsets_c(Dwarf_Debug dbg,Dwarf_Unsigned * debug_info_size,Dwarf_Unsigned * debug_abbrev_size,Dwarf_Unsigned * debug_line_size,Dwarf_Unsigned * debug_loc_size,Dwarf_Unsigned * debug_aranges_size,Dwarf_Unsigned * debug_macinfo_size,Dwarf_Unsigned * debug_pubnames_size,Dwarf_Unsigned * debug_str_size,Dwarf_Unsigned * debug_frame_size,Dwarf_Unsigned * debug_ranges_size,Dwarf_Unsigned * debug_typenames_size,Dwarf_Unsigned * debug_types_size,Dwarf_Unsigned * debug_macro_size,Dwarf_Unsigned * debug_str_offsets_size,Dwarf_Unsigned * debug_sup_size,Dwarf_Unsigned * debug_cu_index_size,Dwarf_Unsigned * debug_tu_index_size)1986 dwarf_get_section_max_offsets_c(Dwarf_Debug dbg,
1987 Dwarf_Unsigned * debug_info_size,
1988 Dwarf_Unsigned * debug_abbrev_size,
1989 Dwarf_Unsigned * debug_line_size,
1990 Dwarf_Unsigned * debug_loc_size,
1991 Dwarf_Unsigned * debug_aranges_size,
1992 Dwarf_Unsigned * debug_macinfo_size,
1993 Dwarf_Unsigned * debug_pubnames_size,
1994 Dwarf_Unsigned * debug_str_size,
1995 Dwarf_Unsigned * debug_frame_size,
1996 Dwarf_Unsigned * debug_ranges_size,
1997 Dwarf_Unsigned * debug_typenames_size,
1998 Dwarf_Unsigned * debug_types_size,
1999 Dwarf_Unsigned * debug_macro_size,
2000 Dwarf_Unsigned * debug_str_offsets_size,
2001 Dwarf_Unsigned * debug_sup_size,
2002 Dwarf_Unsigned * debug_cu_index_size,
2003 Dwarf_Unsigned * debug_tu_index_size)
2004 {
2005 *debug_info_size = dbg->de_debug_info.dss_size;
2006 *debug_abbrev_size = dbg->de_debug_abbrev.dss_size;
2007 *debug_line_size = dbg->de_debug_line.dss_size;
2008 *debug_loc_size = dbg->de_debug_loc.dss_size;
2009 *debug_aranges_size = dbg->de_debug_aranges.dss_size;
2010 *debug_macinfo_size = dbg->de_debug_macinfo.dss_size;
2011 *debug_pubnames_size = dbg->de_debug_pubnames.dss_size;
2012 *debug_str_size = dbg->de_debug_str.dss_size;
2013 *debug_frame_size = dbg->de_debug_frame.dss_size;
2014 *debug_ranges_size = dbg->de_debug_ranges.dss_size;
2015 *debug_typenames_size = dbg->de_debug_typenames.dss_size;
2016 *debug_types_size = dbg->de_debug_types.dss_size;
2017 *debug_macro_size = dbg->de_debug_macro.dss_size;
2018 *debug_str_offsets_size = dbg->de_debug_str_offsets.dss_size;
2019 *debug_sup_size = dbg->de_debug_sup.dss_size;
2020 *debug_cu_index_size = dbg->de_debug_cu_index.dss_size;
2021 *debug_tu_index_size = dbg->de_debug_tu_index.dss_size;
2022 return DW_DLV_OK;
2023 }
2024
2025 /* Now with final sections new to DWARF5 (final) */
2026 int
dwarf_get_section_max_offsets_d(Dwarf_Debug dbg,Dwarf_Unsigned * debug_info_size,Dwarf_Unsigned * debug_abbrev_size,Dwarf_Unsigned * debug_line_size,Dwarf_Unsigned * debug_loc_size,Dwarf_Unsigned * debug_aranges_size,Dwarf_Unsigned * debug_macinfo_size,Dwarf_Unsigned * debug_pubnames_size,Dwarf_Unsigned * debug_str_size,Dwarf_Unsigned * debug_frame_size,Dwarf_Unsigned * debug_ranges_size,Dwarf_Unsigned * debug_typenames_size,Dwarf_Unsigned * debug_types_size,Dwarf_Unsigned * debug_macro_size,Dwarf_Unsigned * debug_str_offsets_size,Dwarf_Unsigned * debug_sup_size,Dwarf_Unsigned * debug_cu_index_size,Dwarf_Unsigned * debug_tu_index_size,Dwarf_Unsigned * debug_names_size,Dwarf_Unsigned * debug_loclists_size,Dwarf_Unsigned * debug_rnglists_size)2027 dwarf_get_section_max_offsets_d(Dwarf_Debug dbg,
2028 Dwarf_Unsigned * debug_info_size,
2029 Dwarf_Unsigned * debug_abbrev_size,
2030 Dwarf_Unsigned * debug_line_size,
2031 Dwarf_Unsigned * debug_loc_size,
2032 Dwarf_Unsigned * debug_aranges_size,
2033 Dwarf_Unsigned * debug_macinfo_size,
2034 Dwarf_Unsigned * debug_pubnames_size,
2035 Dwarf_Unsigned * debug_str_size,
2036 Dwarf_Unsigned * debug_frame_size,
2037 Dwarf_Unsigned * debug_ranges_size,
2038 Dwarf_Unsigned * debug_typenames_size,
2039 Dwarf_Unsigned * debug_types_size,
2040 Dwarf_Unsigned * debug_macro_size,
2041 Dwarf_Unsigned * debug_str_offsets_size,
2042 Dwarf_Unsigned * debug_sup_size,
2043 Dwarf_Unsigned * debug_cu_index_size,
2044 Dwarf_Unsigned * debug_tu_index_size,
2045 Dwarf_Unsigned * debug_names_size,
2046 Dwarf_Unsigned * debug_loclists_size,
2047 Dwarf_Unsigned * debug_rnglists_size)
2048 {
2049 *debug_info_size = dbg->de_debug_info.dss_size;
2050 *debug_abbrev_size = dbg->de_debug_abbrev.dss_size;
2051 *debug_line_size = dbg->de_debug_line.dss_size;
2052 *debug_loc_size = dbg->de_debug_loc.dss_size;
2053 *debug_aranges_size = dbg->de_debug_aranges.dss_size;
2054 *debug_macinfo_size = dbg->de_debug_macinfo.dss_size;
2055 *debug_pubnames_size = dbg->de_debug_pubnames.dss_size;
2056 *debug_str_size = dbg->de_debug_str.dss_size;
2057 *debug_frame_size = dbg->de_debug_frame.dss_size;
2058 *debug_ranges_size = dbg->de_debug_ranges.dss_size;
2059 *debug_typenames_size = dbg->de_debug_typenames.dss_size;
2060 *debug_types_size = dbg->de_debug_types.dss_size;
2061 *debug_macro_size = dbg->de_debug_macro.dss_size;
2062 *debug_str_offsets_size = dbg->de_debug_str_offsets.dss_size;
2063 *debug_sup_size = dbg->de_debug_sup.dss_size;
2064 *debug_cu_index_size = dbg->de_debug_cu_index.dss_size;
2065 *debug_tu_index_size = dbg->de_debug_tu_index.dss_size;
2066 *debug_names_size = dbg->de_debug_names.dss_size;
2067 *debug_loclists_size = dbg->de_debug_loclists.dss_size;
2068 *debug_rnglists_size = dbg->de_debug_rnglists.dss_size;
2069 return DW_DLV_OK;
2070 }
2071
2072 /* Given a section name, get its size and address */
2073 int
dwarf_get_section_info_by_name(Dwarf_Debug dbg,const char * section_name,Dwarf_Addr * section_addr,Dwarf_Unsigned * section_size,Dwarf_Error * error)2074 dwarf_get_section_info_by_name(Dwarf_Debug dbg,
2075 const char *section_name,
2076 Dwarf_Addr *section_addr,
2077 Dwarf_Unsigned *section_size,
2078 Dwarf_Error * error)
2079 {
2080 struct Dwarf_Obj_Access_Section_s doas;
2081 struct Dwarf_Obj_Access_Interface_s * obj = 0;
2082 Dwarf_Unsigned section_count = 0;
2083 Dwarf_Half section_index = 0;
2084
2085 *section_addr = 0;
2086 *section_size = 0;
2087
2088 obj = dbg->de_obj_file;
2089 if (NULL == obj) {
2090 return DW_DLV_NO_ENTRY;
2091 }
2092
2093 section_count = obj->methods->get_section_count(obj->object);
2094
2095 /* We can skip index 0 when considering ELF files, but not other
2096 object types. */
2097 for (section_index = 0; section_index < section_count;
2098 ++section_index) {
2099 int err = 0;
2100 int res = obj->methods->get_section_info(obj->object,
2101 section_index, &doas, &err);
2102 if (res == DW_DLV_ERROR) {
2103 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
2104 }
2105
2106 if (!strcmp(section_name,doas.name)) {
2107 *section_addr = doas.addr;
2108 *section_size = doas.size;
2109 return DW_DLV_OK;
2110 }
2111 }
2112
2113 return DW_DLV_NO_ENTRY;
2114 }
2115
2116 /* Given a section index, get its size and address */
2117 int
dwarf_get_section_info_by_index(Dwarf_Debug dbg,int section_index,const char ** section_name,Dwarf_Addr * section_addr,Dwarf_Unsigned * section_size,Dwarf_Error * error)2118 dwarf_get_section_info_by_index(Dwarf_Debug dbg,
2119 int section_index,
2120 const char **section_name,
2121 Dwarf_Addr *section_addr,
2122 Dwarf_Unsigned *section_size,
2123 Dwarf_Error * error)
2124 {
2125 *section_addr = 0;
2126 *section_size = 0;
2127 *section_name = NULL;
2128
2129 /* Check if we have a valid section index */
2130 if (section_index >= 0 && section_index < dwarf_get_section_count(dbg)) {
2131 int res = 0;
2132 int err = 0;
2133 struct Dwarf_Obj_Access_Section_s doas;
2134 struct Dwarf_Obj_Access_Interface_s * obj = dbg->de_obj_file;
2135 if (NULL == obj) {
2136 return DW_DLV_NO_ENTRY;
2137 }
2138 res = obj->methods->get_section_info(obj->object,
2139 section_index, &doas, &err);
2140 if (res == DW_DLV_ERROR){
2141 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
2142 }
2143
2144 *section_addr = doas.addr;
2145 *section_size = doas.size;
2146 *section_name = doas.name;
2147 return DW_DLV_OK;
2148 }
2149 return DW_DLV_NO_ENTRY;
2150 }
2151
2152 /* Get section count */
2153 int
dwarf_get_section_count(Dwarf_Debug dbg)2154 dwarf_get_section_count(Dwarf_Debug dbg)
2155 {
2156 struct Dwarf_Obj_Access_Interface_s * obj = dbg->de_obj_file;
2157 if (NULL == obj) {
2158 /* -1 */
2159 return DW_DLV_NO_ENTRY;
2160 }
2161 return obj->methods->get_section_count(obj->object);
2162 }
2163
2164 Dwarf_Cmdline_Options dwarf_cmdline_options = {
2165 FALSE /* Use quiet mode by default. */
2166 };
2167
2168 /* Lets libdwarf reflect a command line option, so we can get details
2169 of some errors printed using libdwarf-internal information. */
2170 void
dwarf_record_cmdline_options(Dwarf_Cmdline_Options options)2171 dwarf_record_cmdline_options(Dwarf_Cmdline_Options options)
2172 {
2173 dwarf_cmdline_options = options;
2174 }
2175