Debian Codegraph @

src/lib/libdwarf/dwarf_init_finish.c

raw
1 /*
2 Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2008-2010 Arxan Technologies, Inc. All Rights Reserved.
4 Portions Copyright (C) 2009-2023 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
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 <stdlib.h> /* calloc() free() */
35 #include <string.h> /* memset() strcmp() strncmp() strlen() */
36 #include <stdio.h> /* debugging */
37
38 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
39 #include "stdafx.h"
40 #endif /* HAVE_STDAFX_H */
41
42 #include "dwarf.h"
43 #include "libdwarf.h"
44 #include "libdwarf_private.h"
45 #include "dwarf_base_types.h"
46 #include "dwarf_opaque.h"
47 #include "dwarf_alloc.h"
48 #include "dwarf_error.h"
49 #include "dwarf_util.h"
50 #include "dwarf_memcpy_swap.h"
51 #include "dwarf_harmless.h"
52 #include "dwarf_string.h"
53 #include "dwarf_secname_ck.h"
54 #include "dwarf_setup_sections.h"
55
56 #ifdef HAVE_ZLIB_H
57 #include "zlib.h"
58 #endif
59 #ifdef HAVE_ZSTD_H
60 #include "zstd.h"
61 #endif
62
63 #ifndef ELFCOMPRESS_ZLIB
64 #define ELFCOMPRESS_ZLIB 1
65 #endif
66 #ifndef ELFCOMPRESS_ZSTD
67 #define ELFCOMPRESS_ZSTD 2
68 #endif
69
70 /* If your mingw elf.h is missing SHT_RELA and you do not
71 need SHT_RELA support
72 this define should work for you.
73 It is the elf value, hopefully it will
74 not cause trouble. If does not work, try -1
75 or something else
76 and let us know what works. */
77 #ifndef SHT_RELA
78 #define SHT_RELA 4
79 #endif
80 #ifndef SHT_REL
81 #define SHT_REL 9
82 # endif
83 /* For COMDAT GROUPS. Guarantees we can compile. We hope. */
84 #ifndef SHT_GROUP
85 #define SHT_GROUP 17
86 #endif
87
88 #ifndef SHF_COMPRESSED
89 /* This from ubuntu xenial. Is in top of trunk binutils
90 as of February 2016. Elf Section Flag */
91 #define SHF_COMPRESSED (1 << 11)
92 #endif
93
94 /* This static is copied to the dbg on dbg init
95 so that the static need not be referenced at
96 run time, preserving better locality of
97 reference.
98 Value is 0 means do the string check.
99 Value non-zero means do not do the check.
100 */
101 static Dwarf_Small _dwarf_assume_string_in_bounds;
102 static Dwarf_Small _dwarf_apply_relocs = 1;
103
104 /* Call this after calling dwarf_init but before doing anything else.
105 It applies to all objects, not just the current object. */
106 int
107 dwarf_set_reloc_application(int apply)
108 {
109 int oldval = _dwarf_apply_relocs;
110 _dwarf_apply_relocs = (Dwarf_Small)apply;
111 return oldval;
112 }
113
114 int
115 dwarf_set_stringcheck(int newval)
116 {
117 int oldval = _dwarf_assume_string_in_bounds;
118
119 _dwarf_assume_string_in_bounds = (Dwarf_Small)newval;
120 return oldval;
121 }
122
123 /* Unifies the basic duplicate/empty testing and section
124 data setting to one place. */
125 static int
126 get_basic_section_data(Dwarf_Debug dbg,
127 struct Dwarf_Section_s *secdata,
128 struct Dwarf_Obj_Access_Section_a_s *doas,
129 Dwarf_Unsigned section_index,
130 unsigned group_number,
131 Dwarf_Error* error,
132 int duperr, int emptyerr )
133 {
134 /* There is an elf convention that section index 0 is reserved,
135 and that section is always empty.
136 Non-elf object formats must honor that by ensuring that
137 (when they assign numbers to 'sections' or
138 'section-like-things')
139 they never assign a real section section-number
140 0 to dss_index. */
141 if (secdata->dss_index != 0) {
142 DWARF_DBG_ERROR(dbg, duperr, DW_DLV_ERROR);
143 }
144 if (doas->as_size == 0) {
145 /* As of 2018 it seems impossible to detect
146 (via dwarfdump) whether emptyerr has any
147 practical effect, whether TRUE or FALSE. */
148 if (emptyerr == 0 ) {
149 /* Allow empty section. */
150 return DW_DLV_OK;
151 }
152 /* Know no reason to allow section */
153 DWARF_DBG_ERROR(dbg, emptyerr, DW_DLV_ERROR);
154 }
155 secdata->dss_index = section_index;
156 secdata->dss_size = doas->as_size;
157 secdata->dss_group_number = group_number;
158 secdata->dss_addr = doas->as_addr;
159 secdata->dss_link = doas->as_link;
160 secdata->dss_flags = doas->as_flags;
161 if (secdata->dss_flags & SHF_COMPRESSED) {
162 secdata->dss_shf_compressed = TRUE;
163 }
164 secdata->dss_entrysize = doas->as_entrysize;
165 secdata->dss_addralign = doas->as_addralign;
166 return DW_DLV_OK;
167 }
168
169 static void
170 add_relx_data_to_secdata( struct Dwarf_Section_s *secdata,
171 struct Dwarf_Obj_Access_Section_a_s *doas,
172 Dwarf_Unsigned section_index,
173 int is_rela)
174 {
175 secdata->dss_reloc_index = section_index;
176 secdata->dss_reloc_size = doas->as_size;
177 secdata->dss_reloc_entrysize = doas->as_entrysize;
178 secdata->dss_reloc_addr = doas->as_addr;
179 secdata->dss_reloc_symtab = doas->as_link;
180 secdata->dss_reloc_link = doas->as_link;
181 secdata->dss_is_rela = (char)is_rela;
182 }
183
184 #if 0 /* dump_bytes */
185 static void
186 dump_bytes(const char *msg,Dwarf_Small * start, long len)
187 {
188 Dwarf_Small *end = start + len;
189 Dwarf_Small *cur = start;
190
191 printf("dump_bytes: %s ",msg);
192 for (; cur < end; cur++) {
193 printf("%02x",*cur);
194 }
195 printf("\n");
196 }
197
198 static int
199 all_sig8_bits_zero(Dwarf_Sig8 *val)
200 {
201 unsigned u = 0;
202 for ( ; u < sizeof(*val); ++u) {
203 if (val->signature[u] != 0) {
204 return FALSE;
205 }
206 }
207 return TRUE;
208 }
209 #endif /*0*/
210
211 static int
212 is_section_name_known_already(Dwarf_Debug dbg, const char *scn_name)
213 {
214 unsigned i = 0;
215 for ( ; i < dbg->de_debug_sections_total_entries; ++i) {
216 struct Dwarf_dbg_sect_s *section = &dbg->de_debug_sections[i];
217 if (!strcmp(scn_name, section->ds_name)) {
218 /* The caller will declare this a duplicate, an error. */
219 return DW_DLV_OK;
220 }
221 }
222 /* This is normal, we expect we've not accepted
223 scn_name already. */
224 return DW_DLV_NO_ENTRY;
225 }
226
227 /* Given an Elf ptr, set up dbg with pointers
228 to all the Dwarf data sections.
229 Return NULL on error.
230
231 This function is also responsible for determining
232 whether the given object contains Dwarf information
233 or not. The test currently used is that it contains
234 either a .debug_info or a .debug_frame section. If
235 not, it returns DW_DLV_NO_ENTRY causing dwarf_init() also to
236 return DW_DLV_NO_ENTRY. Earlier, we had thought of using only
237 the presence/absence of .debug_info to test, but we
238 added .debug_frame since there could be stripped objects
239 that have only a .debug_frame section for exception
240 processing.
241 DW_DLV_NO_ENTRY or DW_DLV_OK or DW_DLV_ERROR
242
243 This does not allow for section-groups in object files,
244 for which many .debug_info (and other DWARF) sections may exist.
245
246 We process. .rela (SHT_RELA) and .rel (SHT_REL)
247 sections because with .rela the referencing section
248 offset value is zero whereas with .rel the
249 referencing section value is already correct for
250 the object itself. In other words, we do it because
251 of the definition of .rela relocations in Elf.
252
253 However! In some cases clang emits a .rel section (at least
254 for .rel.debug_info) where symtab entries have an st_value
255 that must be treated like an addend: the compiler did not
256 bother to backpatch the DWARF information for these.
257 */
258
259 /* For an object file with an incorrect rela section name,
260 readelf prints correct debug information,
261 as the tool takes the section type instead
262 of the section name. So check the
263 section name but test section type. */
264 static int
265 is_a_relx_section(const char *scn_name,int type,int *is_rela)
266 {
267 if (type == SHT_RELA) {
268 *is_rela = TRUE;
269 return TRUE;
270 }
271 if (_dwarf_startswith(scn_name,".rela.")) {
272 *is_rela = TRUE;
273 return TRUE;
274 }
275 if (_dwarf_startswith(scn_name,".rel.")) {
276 *is_rela = FALSE;
277 return TRUE;
278 }
279 if (type == SHT_REL) {
280 *is_rela = FALSE;
281 return TRUE;
282 }
283 *is_rela = FALSE;
284 return FALSE;
285 }
286
287 /* ASSERT: names like .debug_ or .zdebug_ never passed in here! */
288 static int
289 is_a_special_section_semi_dwarf(const char *scn_name)
290 {
291 if (!strcmp(scn_name,".strtab") ||
292 !strcmp(scn_name,".symtab")) {
293 return TRUE;
294 }
295 /* It's not one of these special sections referenced in
296 the test. */
297 return FALSE;
298 }
299
300 static int
301 this_section_dwarf_relevant(const char *scn_name,
302 int type,
303 int *is_rela)
304 {
305 /* A small helper function for _dwarf_setup(). */
306 if (_dwarf_startswith(scn_name, ".zdebug_") ||
307 _dwarf_startswith(scn_name, ".debug_")) {
308 /* standard debug */
309 return TRUE;
310 }
311 if (_dwarf_ignorethissection(scn_name)) {
312 return FALSE;
313 }
314 /* Now check if a special section could be
315 in a section_group, but though seems unlikely. */
316 if (!strcmp(scn_name, ".eh_frame")) {
317 /* This is not really a group related file, but
318 it is harmless to consider it such. */
319 return TRUE;
320 }
321 if (!strcmp(scn_name, ".gnu_debuglink")) {
322 /* This is not a group or DWARF related file, but
323 it is useful for split dwarf. */
324 return TRUE;
325 }
326 if (!strcmp(scn_name, ".note.gnu.build-id")) {
327 /* This is not a group or DWARF related file, but
328 it is useful for split dwarf. */
329 return TRUE;
330 }
331 if (!strcmp(scn_name, ".gdb_index")) {
332 return TRUE;
333 }
334 if (is_a_special_section_semi_dwarf(scn_name)) {
335 return TRUE;
336 }
337 if (is_a_relx_section(scn_name,type,is_rela)) {
338 return TRUE;
339 }
340 /* All sorts of sections are of no interest: .text
341 .rel. and many others. */
342 return FALSE;
343 }
344
345 /* This assumes any non-Elf object files have no SHT_GROUP
346 sections. So this code will not be invoked on non-Elf objects.
347 One supposes this is unlikely to match any non-Elf
348 version of COMDAT. */
349 static int
350 insert_sht_list_in_group_map(Dwarf_Debug dbg,
351 struct Dwarf_Obj_Access_Section_a_s *doas,
352 unsigned comdat_group_number,
353 unsigned section_number,
354 Dwarf_Unsigned section_count,
355 struct Dwarf_Obj_Access_Interface_a_s * obj,
356 unsigned *did_add_map,
357 Dwarf_Error *error)
358 {
359 struct Dwarf_Section_s secdata;
360 Dwarf_Small * data = 0;
361 int res = 0;
362 Dwarf_Small* secend = 0;
363
364 memset(&secdata,0,sizeof(secdata));
365 secdata.dss_size = doas->as_size;
366 secdata.dss_entrysize = doas->as_entrysize;
367 secdata.dss_group_number = 1; /* arbitrary. */
368 secdata.dss_index = section_number;
369 secdata.dss_name = ".group";
370 secdata.dss_standard_name = ".group";
371 secdata.dss_number = section_number;
372 secdata.dss_ignore_reloc_group_sec = TRUE;
373 res = _dwarf_load_section(dbg,&secdata,error);
374 if (res != DW_DLV_OK) {
375 if (secdata.dss_data_was_malloc) {
376 free(secdata.dss_data);
377 secdata.dss_data = 0;
378 }
379 return res;
380 }
381 if (!secdata.dss_data) {
382 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
383 return DW_DLV_ERROR;
384 }
385 if (doas->as_entrysize != 4) {
386 if (secdata.dss_data_was_malloc) {
387 free(secdata.dss_data);
388 secdata.dss_data = 0;
389 }
390 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
391 return DW_DLV_ERROR;
392 }
393 /* So now pick up the data in dss_data.
394 It is an array of 32 bit fields.
395 Entry zero is just a constant 1.
396 Each additional is a section number. */
397 data = secdata.dss_data;
398 secend = data + secdata.dss_size;
399 {
400 Dwarf_Unsigned i = 1;
401 Dwarf_Unsigned count = doas->as_size/doas->as_entrysize;
402 Dwarf_Unsigned fval = 0;
403
404 /* The fields treatments with regard
405 to endianness is unclear. In any case a single
406 bit should be on, as 0x01000000
407 without any endiannes swapping.
408 Or so it seems given limited evidence.
409 We read with length checking and allow the
410 reader to byte swap and then fix things.
411 At least one test case has big-endian
412 data but little-endian SHT_GROUP data. */
413 if ((data+DWARF_32BIT_SIZE) > secend) {
414 /* Duplicates the check in READ_UNALIGNED_CK
415 so we can free allocated memory bere. */
416 if (secdata.dss_data_was_malloc) {
417 free(secdata.dss_data);
418 secdata.dss_data = 0;
419 }
420 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
421 return DW_DLV_ERROR;
422 }
423 READ_UNALIGNED_CK(dbg,fval,Dwarf_Unsigned,
424 data,
425 DWARF_32BIT_SIZE,
426 error,
427 secend);
428 if (fval != 1 && fval != 0x1000000) {
429 /* Could be corrupted elf object. */
430 if (secdata.dss_data_was_malloc) {
431 free(secdata.dss_data);
432 secdata.dss_data = 0;
433 }
434 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
435 return DW_DLV_ERROR;
436 }
437
438 data = data + doas->as_entrysize;
439 for (i = 1 ; i < count ; ++i) {
440 Dwarf_Unsigned val = 0;
441
442 if ((data+DWARF_32BIT_SIZE) > secend) {
443 /* Duplicates the check in READ_UNALIGNED_CK
444 so we can free allocated memory bere. */
445 if (secdata.dss_data_was_malloc) {
446 free(secdata.dss_data);
447 secdata.dss_data = 0;
448 }
449 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
450 return DW_DLV_ERROR;
451 }
452 READ_UNALIGNED_CK(dbg,val,Dwarf_Unsigned,
453 data,
454 DWARF_32BIT_SIZE,
455 error,
456 secend);
457 if (val > section_count) {
458 /* Might be confused endianness by
459 the compiler generating the SHT_GROUP.
460 This is pretty horrible. */
461 Dwarf_Unsigned valr = 0;
462 _dwarf_memcpy_swap_bytes(&valr,&val,
463 DWARF_32BIT_SIZE);
464 if (valr > section_count) {
465 if (secdata.dss_data_was_malloc) {
466 free(secdata.dss_data);
467 secdata.dss_data = 0;
468 }
469 _dwarf_error(dbg,error,
470 DW_DLE_GROUP_INTERNAL_ERROR);
471 return DW_DLV_ERROR;
472 }
473 /* Ok. Yes, ugly. */
474 val = valr;
475 }
476 {
477 /* Ensure this group entry DWARF relevant before
478 adding to group map */
479 struct Dwarf_Obj_Access_Section_a_s doasx;
480 int resx = DW_DLV_ERROR;
481 int err = 0;
482 int is_rela = FALSE;
483
484 memset(&doasx,0,sizeof(doasx));
485 resx = obj->ai_methods->
486 om_get_section_info(obj->ai_object,
487 val,
488 &doasx, &err);
489 if (resx == DW_DLV_NO_ENTRY){
490 /* Should we really ignore this? */
491 continue;
492 }
493 if (resx == DW_DLV_ERROR){
494 if (secdata.dss_data_was_malloc) {
495 free(secdata.dss_data);
496 secdata.dss_data = 0;
497 }
498 _dwarf_error(dbg,error,err);
499 return resx;
500 }
501 if (!this_section_dwarf_relevant(doasx.as_name,
502 (int)doasx.as_type,&is_rela) ) {
503 continue;
504 }
505 data += DWARF_32BIT_SIZE;
506 *did_add_map = TRUE;
507 res = _dwarf_insert_in_group_map(dbg,
508 (unsigned)comdat_group_number,
509 (unsigned)val,
510 doasx.as_name,
511 error);
512 if (res != DW_DLV_OK) {
513 if (secdata.dss_data_was_malloc) {
514 free(secdata.dss_data);
515 secdata.dss_data = 0;
516 }
517 return res;
518 }
519 }
520 }
521 }
522 if (secdata.dss_data_was_malloc) {
523 free(secdata.dss_data);
524 secdata.dss_data = 0;
525 }
526 return DW_DLV_OK;
527 }
528
529 /* Split dwarf CUs can be in an object with non-split
530 or split may be in a separate object.
531 If all in one object the default is to deal with group_number
532 and ignore DW_GROUPNUMBER_DWO.
533 If only .dwo the default is DW_GROUPNUMBER_DWO(2).
534 Otherwise use DW_GROUP_NUMBER_BASE(1).
535
536 If there are COMDAT SHT_GROUP sections, these
537 are assigned group numbers 3-N as needed.
538
539 At present this makes the assumption that COMDAT group
540 (ie, SHT_GROUP) sections
541 have lower section numbers than the sections COMDAT refers to.
542 It is not clear whether this is guaranteed, COMDAT is not
543 an official Elf thing and documentation is scarce.
544 In the 1990's SGI folks and others formed a committee
545 and attempted to get COMDAT and a feature allowing section
546 numbers greater than 16 bits into Elf, but there was no
547 group that was able to approve such things.
548
549 This is called once at dbg init time.
550 */
551
552 static int
553 determine_target_group(Dwarf_Unsigned section_count,
554 struct Dwarf_Obj_Access_Interface_a_s * obj,
555 unsigned *group_number_out,
556 Dwarf_Debug dbg,
557 Dwarf_Error *error)
558 {
559 unsigned obj_section_index = 0;
560 int found_group_one = 0;
561 int found_group_two = 0;
562 struct Dwarf_Group_Data_s *grp = 0;
563 unsigned comdat_group_next = 3;
564 unsigned lowest_comdat_groupnum = 0;
565
566 grp = &dbg->de_groupnumbers;
567 grp->gd_number_of_groups = 0;
568 grp->gd_number_of_sections = (unsigned int)section_count;
569 if (grp->gd_map) {
570 _dwarf_error(dbg,error,DW_DLE_GROUP_INTERNAL_ERROR);
571 return DW_DLV_OK;
572 }
573 for (obj_section_index = 0; obj_section_index < section_count;
574 ++obj_section_index) {
575
576 struct Dwarf_Obj_Access_Section_a_s doas;
577 int res = DW_DLV_ERROR;
578 int err = 0;
579 const char *scn_name = 0;
580 unsigned groupnumber = 0;
581 unsigned mapgroupnumber = 0;
582 int is_rela = FALSE;
583
584 memset(&doas,0,sizeof(doas));
585 res = obj->ai_methods->om_get_section_info(obj->ai_object,
586 obj_section_index,
587 &doas, &err);
588 if (res == DW_DLV_NO_ENTRY){
589 return res;
590 }
591 if (res == DW_DLV_ERROR){
592 _dwarf_error(dbg, error,err);
593 return res;
594 }
595
596 if (doas.as_type == SHT_GROUP) {
597 /* See assumptions in function comment above. */
598 unsigned did_add_map = 0;
599 /* Add to our map. Here we
600 are assuming SHT_GROUP records come first.
601 Till proven wrong. */
602 res = insert_sht_list_in_group_map(dbg,&doas,
603 comdat_group_next,
604 obj_section_index,
605 section_count,
606 obj,
607 &did_add_map,error);
608 if (res != DW_DLV_OK) {
609 return res;
610 }
611 if (!lowest_comdat_groupnum) {
612 lowest_comdat_groupnum = comdat_group_next;
613 }
614 if (did_add_map) {
615 ++grp->gd_number_of_groups;
616 ++comdat_group_next;
617 }
618 continue;
619 }
620 scn_name = doas.as_name;
621 if (!this_section_dwarf_relevant(scn_name,
622 (int)doas.as_type,
623 &is_rela) ) {
624 continue;
625 }
626
627 /* Now at a 'normal' section, though we do not
628 quite know what group it is. */
629
630 res = _dwarf_section_get_target_group_from_map(dbg,
631 obj_section_index,&groupnumber,error);
632 if (res == DW_DLV_OK ) {
633 /* groupnumber is set. Fall through.
634 All COMDAT group should get here. */
635 mapgroupnumber = groupnumber;
636 } else if (res == DW_DLV_ERROR) {
637 return res;
638 } else { /* DW_DLV_NO_ENTRY */
639 /* Normal non-COMDAT. groupnumber is zero. */
640 }
641
642 /* BUILDING_MAP. See also BUILDING_SECTIONS, SETUP_SECTION */
643 if (!groupnumber) {
644 res =_dwarf_dwo_groupnumber_given_name(scn_name,
645 &groupnumber);
646 /* DW_DLV_ERROR impossible here. */
647 if (res == DW_DLV_OK) {
648 /* groupnumber set 2 */
649 } else {
650 /* This is what it has to be.
651 .rela in here too. */
652 groupnumber = DW_GROUPNUMBER_BASE;
653 }
654 }
655 if (is_a_relx_section(scn_name,(int)doas.as_type,
656 &is_rela)) {
657 continue;
658 }
659
660 /* ASSERT: groupnumber non-zero now */
661 if (!is_a_special_section_semi_dwarf(scn_name)) {
662 if (mapgroupnumber) {
663 /* Already in group map */
664 continue;
665 }
666 /* !mapgroupnumber */
667 res = _dwarf_insert_in_group_map(dbg,
668 (unsigned)groupnumber,
669 (unsigned)obj_section_index,
670 scn_name,
671 error);
672 if (res != DW_DLV_OK) {
673 return res;
674 }
675 if (groupnumber == 1) {
676 found_group_one++;
677 } else if (groupnumber == 2) {
678 found_group_two++;
679 } else { /* fall through to continue */ }
680 continue;
681 }
682 }
683 if (found_group_two) {
684 ++grp->gd_number_of_groups;
685 }
686 if (found_group_one) {
687 *group_number_out = DW_GROUPNUMBER_BASE;
688 ++grp->gd_number_of_groups;
689 } else {
690 if (found_group_two) {
691 *group_number_out = DW_GROUPNUMBER_DWO;
692 } else {
693 if (lowest_comdat_groupnum) {
694 *group_number_out = lowest_comdat_groupnum;
695 } else {
696 *group_number_out = DW_GROUPNUMBER_BASE;
697 }
698 }
699 }
700 return DW_DLV_OK;
701 }
702
703 static int
704 _dwarf_setup(Dwarf_Debug dbg, Dwarf_Error * error)
705 {
706 const char *scn_name = 0;
707 struct Dwarf_Obj_Access_Interface_a_s * obj = 0;
708 int resn = 0;
709 struct Dwarf_Section_s **sections = 0;
710 Dwarf_Small endianness = 0;
711 Dwarf_Unsigned section_count = 0;
712 unsigned default_group_number = 0;
713 unsigned foundDwarf = FALSE;
714 Dwarf_Unsigned obj_section_index = 0;
715
716 dbg->de_assume_string_in_bounds =
717 _dwarf_assume_string_in_bounds;
718 /* First make an arbitrary assumption. */
719 dbg->de_copy_word = _dwarf_memcpy_noswap_bytes;
720 obj = dbg->de_obj_file;
721 endianness = obj->ai_methods->om_get_byte_order(obj->ai_object);
722 /* Then adjust any changes we need. */
723 #ifdef WORDS_BIGENDIAN
724 dbg->de_big_endian_object = 1;
725 if (endianness == DW_END_little) {
726 dbg->de_big_endian_object = 0;
727 dbg->de_copy_word = _dwarf_memcpy_swap_bytes;
728 }
729 #else /* little endian */
730 dbg->de_big_endian_object = 0;
731 if (endianness == DW_END_big ) {
732 dbg->de_big_endian_object = 1;
733 dbg->de_copy_word = _dwarf_memcpy_swap_bytes;
734 }
735 #endif /* !WORDS_BIGENDIAN */
736
737 /* The following de_length_size is Not Too Significant.
738 Only used one calculation, and an approximate one
739 at that. */
740 dbg->de_length_size = obj->ai_methods->
741 om_get_length_size(obj->ai_object);
742 dbg->de_pointer_size =
743 obj->ai_methods->om_get_pointer_size(obj->ai_object);
744 section_count = obj->ai_methods->
745 om_get_section_count(obj->ai_object);
746 resn = determine_target_group(section_count,obj,
747 &default_group_number,dbg,error);
748 if (resn == DW_DLV_ERROR) {
749 return DW_DLV_ERROR;
750 }
751 if (dbg->de_groupnumber == DW_GROUPNUMBER_ANY) {
752 dbg->de_groupnumber = default_group_number;
753 }
754 /* Allocate space to record references to debug sections
755 that can be referenced by RELA sections in
756 the 'sh_info' field. */
757 sections = (struct Dwarf_Section_s **)calloc(section_count + 1,
758 sizeof(struct Dwarf_Section_s *));
759 if (!sections) {
760 /* Impossible case, we hope. Give up. */
761 _dwarf_error(dbg, error, DW_DLE_SECTION_ERROR);
762 return DW_DLV_ERROR;
763 }
764
765 /* We can skip index 0 when considering ELF files, but not other
766 object types. Indeed regardless of the object type we should
767 skip section 0 here.
768 This is a convention. We depend on it.
769 Non-elf object access code should
770 (in itself) understand we will index beginning at 1 and adjust
771 itself to deal with this Elf convention. Without this
772 convention various parts of the code in this file won't
773 work correctly.
774 A dss_index of 0 must not be used, even though we start at 0
775 here. So the get_section_info() must adapt to the situation
776 (the elf version does automatically as a result of Elf having
777 a section zero with zero length and an empty name). */
778
779 /* ASSERT: all group map entries set up. */
780
781 for (obj_section_index = 0; obj_section_index < section_count;
782 ++obj_section_index) {
783
784 struct Dwarf_Obj_Access_Section_a_s doas;
785 int res = DW_DLV_ERROR;
786 int err = 0;
787 unsigned groupnumber = 0;
788 unsigned mapgroupnumber = 0;
789 int is_rela = FALSE;
790
791 res = _dwarf_section_get_target_group_from_map(dbg,
792 (unsigned int)obj_section_index, &groupnumber,error);
793 if (res == DW_DLV_OK ) {
794 /* groupnumber is set. Fall through */
795 mapgroupnumber = groupnumber;
796 } else if (res == DW_DLV_ERROR) {
797 free(sections);
798 return res;
799 } else { /* DW_DLV_NO_ENTRY */
800 /* fall through, a BASE or DWO group, possibly */
801 }
802 memset(&doas,0,sizeof(doas));
803
804 res = obj->ai_methods->om_get_section_info(obj->ai_object,
805 obj_section_index,
806 &doas, &err);
807 if (res == DW_DLV_NO_ENTRY){
808 free(sections);
809 return res;
810 }
811 if (res == DW_DLV_ERROR){
812 free(sections);
813 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
814 }
815 scn_name = doas.as_name;
816 if (!groupnumber) {
817 /* This finds dwo sections, group 2 */
818 res = _dwarf_dwo_groupnumber_given_name(scn_name,
819 &groupnumber);
820 if (res == DW_DLV_NO_ENTRY) {
821 /* No, must be group 1 */
822 groupnumber = DW_GROUPNUMBER_BASE;
823 }
824 }
825 if (!this_section_dwarf_relevant(scn_name,
826 (int)doas.as_type,
827 &is_rela) ) {
828 continue;
829 }
830 if (!is_a_relx_section(scn_name,(int)doas.as_type,
831 &is_rela)
832 && !is_a_special_section_semi_dwarf(scn_name)) {
833 /* We do these actions only for group-related
834 sections. Do for .debug_info etc,
835 never for .strtab or .rela.*
836 We already tested for relevance, so that part
837 is not news. */
838 if (mapgroupnumber == dbg->de_groupnumber) {
839 /* OK. Mapped. Part of the group.. This will
840 catch the cases where there are versions of
841 a section in multiple COMDATs and in BASE
842 an DWO to get the right one */
843 } else {
844 /* This section not mapped into this group. */
845 if (groupnumber == 1 && dbg->de_groupnumber > 2 &&
846 !_dwarf_section_in_group_by_name(dbg,scn_name,
847 dbg->de_groupnumber)) {
848 /* Load the section (but as group 1) */
849 } else {
850 continue;
851 }
852 }
853 }
854 /* BUILDING_SECTIONS. See also BUILDING_MAP, SETUP_SECTION */
855 {
856 /* Build up the sections table and the
857 de_debug* etc pointers in Dwarf_Debug. */
858 struct Dwarf_dbg_sect_s *section = 0;
859 int found_match = FALSE;
860
861 res = is_section_name_known_already(dbg,scn_name);
862 if (res == DW_DLV_OK) {
863 #if 0 /* Removed check for section duplication */
864 /* DUPLICATE */
865 DWARF_DBG_ERROR(dbg, DW_DLE_SECTION_DUPLICATION,
866 DW_DLV_ERROR);
867 /* Metrowerks does this nonsense */
868 #endif
869 continue;
870 }
871 if (res == DW_DLV_ERROR) {
872 free(sections);
873 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
874 }
875 /* No entry: new-to-us section, the normal case. */
876 res = _dwarf_enter_section_in_de_debug_sections_array(dbg,
877 scn_name, obj_section_index, groupnumber,&err);
878 if (res == DW_DLV_OK) {
879 section = &dbg->de_debug_sections[
880 dbg->de_debug_sections_total_entries-1];
881 res = get_basic_section_data(dbg,
882 section->ds_secdata, &doas,
883 obj_section_index,
884 groupnumber,
885 error,
886 section->ds_duperr,
887 section->ds_emptyerr);
888 if (res != DW_DLV_OK) {
889 free(sections);
890 return res;
891 }
892 sections[obj_section_index] = section->ds_secdata;
893 foundDwarf += section->ds_have_dwarf;
894 found_match = TRUE;
895 /* Normal section set up.
896 Fall through. */
897 } else if (res == DW_DLV_NO_ENTRY) {
898 /* We get here for relocation sections.
899 Fall through. */
900 } else {
901 free(sections);
902 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
903 }
904
905 if (!found_match) {
906 /* For an object file with incorrect rel[a]
907 section name, the 'readelf' tool,
908 prints correct debug information,
909 as the tool takes the section type instead
910 of the section name. If the current section
911 is a RELA one and the 'sh_info'
912 refers to a debug section, add the
913 relocation data. */
914 if (is_a_relx_section(scn_name,
915 (int)doas.as_type, &is_rela)) {
916 if ( doas.as_info < section_count) {
917 if (sections[doas.as_info]) {
918 add_relx_data_to_secdata(
919 sections[doas.as_info],
920 &doas,
921 obj_section_index,is_rela);
922 }
923 } else {
924 /* Something is wrong with the ELF file. */
925 free(sections);
926 DWARF_DBG_ERROR(dbg, DW_DLE_ELF_SECT_ERR,
927 DW_DLV_ERROR);
928 }
929 }
930 }
931 /* Fetch next section */
932 }
933 }
934
935 /* Free table with section information. */
936 free(sections);
937 if (foundDwarf) {
938 return DW_DLV_OK;
939 }
940 return DW_DLV_NO_ENTRY;
941 }
942
943 /* There is one table per CU and one per TU, and each
944 table refers to the associated other DWARF data
945 for that CU or TU.
946 See DW_SECT_*
947
948 In DWARF4 the type units are in .debug_types
949 In DWARF5 the type units are in .debug_info.
950 */
951
952 static int
953 load_debugfission_tables(Dwarf_Debug dbg,Dwarf_Error *error)
954 {
955 int i = 0;
956 if (dbg->de_debug_cu_index.dss_size ==0 &&
957 dbg->de_debug_tu_index.dss_size ==0) {
958 /* This is the normal case.
959 No debug fission. Not a .dwp object. */
960 return DW_DLV_NO_ENTRY;
961 }
962
963 for (i = 0; i < 2; ++i) {
964 Dwarf_Xu_Index_Header xuptr = 0;
965 struct Dwarf_Section_s* dwsect = 0;
966 Dwarf_Unsigned version = 0;
967 Dwarf_Unsigned number_of_cols /* L */ = 0;
968 Dwarf_Unsigned number_of_CUs /* N */ = 0;
969 Dwarf_Unsigned number_of_slots /* M */ = 0;
970 const char *secname = 0;
971 int res = 0;
972 const char *type = 0;
973
974 if (i == 0) {
975 dwsect = &dbg->de_debug_cu_index;
976 type = "cu";
977 } else {
978 dwsect = &dbg->de_debug_tu_index;
979 type = "tu";
980 }
981 if ( !dwsect->dss_size ) {
982 continue;
983 }
984 res = dwarf_get_xu_index_header(dbg,type,
985 &xuptr,&version,&number_of_cols,
986 &number_of_CUs,&number_of_slots,
987 &secname,error);
988 if (res == DW_DLV_NO_ENTRY) {
989 continue;
990 }
991 if (res != DW_DLV_OK) {
992 return res;
993 }
994 if (i == 0) {
995 dbg->de_cu_hashindex_data = xuptr;
996 } else {
997 dbg->de_tu_hashindex_data = xuptr;
998 }
999 }
1000 return DW_DLV_OK;
1001 }
1002
1003 /*
1004 Use a Dwarf_Obj_Access_Interface to kick things off.
1005 All other init routines eventually use this one.
1006 The returned Dwarf_Debug contains a copy of *obj
1007 the callers copy of *obj may be freed whenever the caller
1008 wishes.
1009
1010 New March 2017. Enables dealing with DWARF5 split
1011 dwarf more fully. */
1012 int
1013 dwarf_object_init_b(Dwarf_Obj_Access_Interface_a* obj,
1014 Dwarf_Handler errhand,
1015 Dwarf_Ptr errarg,
1016 unsigned groupnumber,
1017 Dwarf_Debug* ret_dbg,
1018 Dwarf_Error* error)
1019 {
1020 Dwarf_Debug dbg = 0;
1021 int setup_result = DW_DLV_OK;
1022 Dwarf_Unsigned filesize = 0;
1023
1024 if (!ret_dbg) {
1025 DWARF_DBG_ERROR(NULL,DW_DLE_DWARF_INIT_DBG_NULL,
1026 DW_DLV_ERROR);
1027 }
1028 /* Non-null *ret_dbg will cause problems dealing with
1029 DW_DLV_ERROR */
1030 *ret_dbg = 0;
1031 filesize = obj->ai_methods->om_get_filesize(obj->ai_object);
1032 /* Initializes Dwarf_Debug struct and returns
1033 a pointer to that empty record.
1034 Filesize is to set up a sensible default hash tree
1035 size. */
1036 dbg = _dwarf_get_debug(filesize);
1037 if (IS_INVALID_DBG(dbg)) {
1038 dwarf_finish(dbg);
1039 dbg = 0;
1040 DWARF_DBG_ERROR(dbg, DW_DLE_DBG_ALLOC, DW_DLV_ERROR);
1041 }
1042 dbg->de_errhand = errhand;
1043 dbg->de_errarg = errarg;
1044 dbg->de_frame_rule_initial_value = DW_FRAME_REG_INITIAL_VALUE;
1045 dbg->de_frame_reg_rules_entry_count = DW_FRAME_LAST_REG_NUM;
1046 dbg->de_frame_cfa_col_number = DW_FRAME_CFA_COL3;
1047 dbg->de_frame_same_value_number = DW_FRAME_SAME_VAL;
1048 dbg->de_frame_undefined_value_number = DW_FRAME_UNDEFINED_VAL;
1049 dbg->de_dbg = dbg;
1050 /* See dwarf_set_tied_dbg() dwarf_get_tied_dbg()
1051 and comments in dwarf_opaque.h*/
1052 dbg->de_primary_dbg = dbg;
1053 dbg->de_secondary_dbg = 0;
1054 dbg->de_errors_dbg = dbg;
1055
1056 dbg->de_obj_file = obj;
1057 dbg->de_filesize = filesize;
1058 dbg->de_groupnumber = groupnumber;
1059 setup_result = _dwarf_setup(dbg, error);
1060 if (setup_result == DW_DLV_OK) {
1061 int fission_result = load_debugfission_tables(dbg,error);
1062 /* In most cases we get
1063 setup_result == DW_DLV_NO_ENTRY here
1064 as having debugfission (.dwp objects)
1065 is fairly rare. */
1066 if (fission_result == DW_DLV_ERROR) {
1067 /* Something is very wrong. */
1068 setup_result = fission_result;
1069 }
1070 if (setup_result == DW_DLV_OK) {
1071 _dwarf_harmless_init(&dbg->de_harmless_errors,
1072 DW_HARMLESS_ERROR_CIRCULAR_LIST_DEFAULT_SIZE);
1073 *ret_dbg = dbg;
1074 /* This is the normal return. */
1075 return setup_result;
1076 }
1077 }
1078 if (setup_result == DW_DLV_NO_ENTRY) {
1079 _dwarf_free_all_of_one_debug(dbg);
1080 dbg = 0;
1081 /* ASSERT: _dwarf_free_all_of_one_debug() never returns
1082 DW_DLV_ERROR */
1083 return setup_result;
1084 }
1085 /* An error of some sort. Report it as well as
1086 possible.
1087 ASSERT: setup_result == DW_DLV_ERROR
1088 here */
1089 {
1090 Dwarf_Unsigned myerr = 0;
1091 dwarfstring msg;
1092
1093 dwarfstring_constructor(&msg);
1094 /* We cannot use any _dwarf_setup()
1095 error here as
1096 we are freeing dbg, making that error (setup
1097 as part of dbg) stale.
1098 Hence we have to make a new error without a dbg.
1099 But error might be NULL and the init call
1100 error-handler function might be set.
1101 */
1102 if (error && *error) {
1103 /* Preserve our _dwarf_setup error number, but
1104 this does not apply if error NULL. */
1105 /* *error safe */
1106 myerr = dwarf_errno(*error);
1107 /* *error safe */
1108 dwarfstring_append(&msg,dwarf_errmsg(*error));
1109 /* deallocate the soon-stale error pointer. */
1110 dwarf_dealloc_error(dbg,*error);
1111 /* *error safe */
1112 *error = 0;
1113 }
1114 /* The status we want to return here is of _dwarf_setup,
1115 not of the _dwarf_free_all_of_one_debug(dbg) call.
1116 So use a local status variable for the free. */
1117 _dwarf_free_all_of_one_debug(dbg);
1118 dbg = 0;
1119 if (myerr) {
1120 /* Use the _dwarf_setup error number.
1121 If error is NULL the following will issue
1122 a message on stderr, as without
1123 dbg there is no error-handler function.
1124 */
1125 _dwarf_error_string(dbg,error,myerr,
1126 dwarfstring_string(&msg));
1127 dwarfstring_destructor(&msg);
1128 } /* else return quietly, a serious error
1129 was already reported. */
1130 }
1131 return setup_result;
1132 }
1133
1134 /* A finish routine that is completely unaware of ELF.
1135
1136 Frees all memory that was not previously freed by
1137 dwarf_dealloc.
1138 NEVER returns DW_DLV_ERROR;
1139
1140 Aside from certain categories. */
1141 int
1142 dwarf_object_finish(Dwarf_Debug dbg)
1143 {
1144 int res = 0;
1145 /* do not use CHECK_DBG */
1146 _dwarf_harmless_cleanout(&dbg->de_harmless_errors);
1147 res = _dwarf_free_all_of_one_debug(dbg);
1148 /* see dwarf_error.h dwarf_error.c Relevant
1149 to trying and failing to open/read corrupt
1150 object files. */
1151 return res;
1152 }
1153
1154 #if defined(HAVE_ZLIB) && defined(HAVE_ZSTD)
1155 /* case 1:
1156 The input stream is assumed to contain
1157 the four letters
1158 ZLIB
1159 Followed by 8 bytes of the size of the
1160 uncompressed stream. Presented as
1161 a big-endian binary number.
1162 Following that is the stream to decompress.
1163
1164 case 2,3:
1165 The section flag bit SHF_COMPRESSED (1 << 11)
1166 must be set.
1167 we then do the equivalent of reading a
1168 Elf32_External_Chdr
1169 or
1170 Elf64_External_Chdr
1171 to get the type (which must be 1 (zlib) or 2 (zstd))
1172 and the decompressed_length.
1173 Then what follows the implicit Chdr is decompressed.
1174
1175 */
1176
1177 /* ALLOWED_ZLIB_INFLATION is a heuristic, not necessarily right.
1178 The test case klingler2/compresseddebug.amd64 actually
1179 inflates about 8 times. */
1180 #define ALLOWED_ZLIB_INFLATION 16
1181 #define ALLOWED_ZSTD_INFLATION 16
1182 static int
1183 do_decompress(Dwarf_Debug dbg,
1184 struct Dwarf_Section_s *section,
1185 Dwarf_Error * error)
1186 {
1187 Dwarf_Small *basesrc = section->dss_data;
1188 Dwarf_Small *src = basesrc;
1189 Dwarf_Small *dest = 0;
1190 Dwarf_Unsigned destlen = 0;
1191 Dwarf_Unsigned srclen = section->dss_size;
1192 Dwarf_Unsigned flags = section->dss_flags;
1193 Dwarf_Small *endsection = 0;
1194 int zstdcompress = FALSE;
1195 Dwarf_Unsigned uncompressed_len = 0;
1196
1197 endsection = basesrc + section->dss_size;
1198 if ((basesrc + 12) > endsection) {
1199 _dwarf_error_string(dbg, error,DW_DLE_ZLIB_SECTION_SHORT,
1200 "DW_DLE_ZLIB_SECTION_SHORT"
1201 "Section too short to be either zlib or zstd related");
1202 return DW_DLV_ERROR;
1203 }
1204 section->dss_compressed_length = srclen;
1205 if (!strncmp("ZLIB",(const char *)src,4)) {
1206 unsigned i = 0;
1207 unsigned l = 8;
1208 unsigned char *c = src+4;
1209 for ( ; i < l; ++i,c++) {
1210 uncompressed_len <<= 8;
1211 uncompressed_len += *c;
1212 }
1213 src = src + 12;
1214 srclen -= 12;
1215 section->dss_uncompressed_length = uncompressed_len;
1216 section->dss_ZLIB_compressed = TRUE;
1217 } else if (flags & SHF_COMPRESSED) {
1218 /* The prefix is a struct:
1219 unsigned int type; followed by pad if following are 64bit!
1220 size-of-target-address size
1221 size-of-target-address
1222 */
1223 Dwarf_Small *ptr = (Dwarf_Small *)src;
1224 Dwarf_Unsigned type = 0;
1225 Dwarf_Unsigned size = 0;
1226 /* Dwarf_Unsigned addralign = 0; */
1227 unsigned fldsize = dbg->de_pointer_size;
1228 unsigned structsize = 3* fldsize;
1229 READ_UNALIGNED_CK(dbg,type,Dwarf_Unsigned,ptr,
1230 DWARF_32BIT_SIZE,
1231 error,endsection);
1232 ptr += fldsize;
1233 READ_UNALIGNED_CK(dbg,size,Dwarf_Unsigned,ptr,fldsize,
1234 error,endsection);
1235 switch(type) {
1236 case ELFCOMPRESS_ZLIB:
1237 break;
1238 case ELFCOMPRESS_ZSTD:
1239 zstdcompress = TRUE;
1240 break;
1241 default: {
1242 char buf[100];
1243 dwarfstring m;
1244
1245 dwarfstring_constructor_static(&m,buf,sizeof(buf));
1246 dwarfstring_append_printf_u(&m,
1247 "DW_DLE_ZDEBUG_INPUT_FORMAT_ODD"
1248 " The SHF_COMPRESSED type field is 0x%x, neither"
1249 " zlib (1) or zstd(2). Corrupt dwarf.", type);
1250 _dwarf_error_string(dbg, error,
1251 DW_DLE_ZDEBUG_INPUT_FORMAT_ODD,
1252 dwarfstring_string(&m));
1253 dwarfstring_destructor(&m);
1254 return DW_DLV_ERROR;
1255 }
1256 }
1257 uncompressed_len = size;
1258 section->dss_uncompressed_length = uncompressed_len;
1259 src += structsize;
1260 srclen -= structsize;
1261 section->dss_shf_compressed = TRUE;
1262 } else {
1263 _dwarf_error_string(dbg, error,
1264 DW_DLE_ZDEBUG_INPUT_FORMAT_ODD,
1265 "DW_DLE_ZDEBUG_INPUT_FORMAT_ODD"
1266 " The compressed section is not properly formatted");
1267 return DW_DLV_ERROR;
1268 }
1269 if (!zstdcompress) {
1270 /* According to zlib.net zlib essentially never expands
1271 the data when compressing. There is no statement
1272 about any effective limit in the compression factor
1273 though we, here, assume such a limit to check
1274 for sanity in the object file.
1275 These tests are heuristics. */
1276 Dwarf_Unsigned max_inflated_len =
1277 srclen*ALLOWED_ZLIB_INFLATION;
1278
1279 if (srclen > 50) {
1280 /* If srclen not super tiny lets check the following. */
1281 if (uncompressed_len < (srclen/2)) {
1282 /* Violates the approximate invariant about
1283 compression not actually inflating. */
1284 _dwarf_error_string(dbg, error,
1285 DW_DLE_ZLIB_UNCOMPRESS_ERROR,
1286 "DW_DLE_ZLIB_UNCOMPRESS_ERROR"
1287 " The zlib compressed section is"
1288 "absurdly small. Corrupt dwarf");
1289 return DW_DLV_ERROR;
1290 }
1291 }
1292 if (max_inflated_len < srclen) {
1293 /* The calculation overflowed. */
1294 _dwarf_error_string(dbg, error,
1295 DW_DLE_ZLIB_UNCOMPRESS_ERROR,
1296 "DW_DLE_ZLIB_UNCOMPRESS_ERROR:"
1297 " The zlib compressed section is"
1298 " absurdly large so arithmentic overflow."
1299 " So corrupt dwarf");
1300 return DW_DLV_ERROR;
1301 }
1302 if (uncompressed_len > max_inflated_len) {
1303 _dwarf_error_string(dbg, error,
1304 DW_DLE_ZLIB_UNCOMPRESS_ERROR,
1305 "DW_DLE_ZLIB_UNCOMPRESS_ERROR"
1306 " The zlib compressed section is"
1307 " absurdly large given the input section"
1308 " length. So corrupt dwarf");
1309 return DW_DLV_ERROR;
1310 }
1311 }
1312 if (zstdcompress) {
1313 /* According to zlib.net zlib essentially never expands
1314 the data when compressing. There is no statement
1315 about any effective limit in the compression factor
1316 though we, here, assume such a limit to check
1317 for sanity in the object file.
1318 These tests are heuristics. */
1319 Dwarf_Unsigned max_inflated_len =
1320 srclen*ALLOWED_ZSTD_INFLATION;
1321
1322 if (srclen > 50) {
1323 /* If srclen not super tiny lets check the following. */
1324 if (uncompressed_len < (srclen/2)) {
1325 /* Violates the approximate invariant about
1326 compression not actually inflating. */
1327 _dwarf_error_string(dbg, error,
1328 DW_DLE_ZLIB_UNCOMPRESS_ERROR,
1329 "DW_DLE_ZLIB_UNCOMPRESS_ERROR"
1330 " The zstd compressed section is"
1331 "absurdly small. Corrupt dwarf");
1332 return DW_DLV_ERROR;
1333 }
1334 }
1335 if (max_inflated_len < srclen) {
1336 /* The calculation overflowed. */
1337 _dwarf_error_string(dbg, error,
1338 DW_DLE_ZLIB_UNCOMPRESS_ERROR,
1339 "DW_DLE_ZLIB_UNCOMPRESS_ERROR"
1340 " The zstd compressed section is"
1341 " absurdly large so arithmentic overflow."
1342 " So corrupt dwarf");
1343 return DW_DLV_ERROR;
1344 }
1345 if (uncompressed_len > max_inflated_len) {
1346 _dwarf_error_string(dbg, error,
1347 DW_DLE_ZLIB_UNCOMPRESS_ERROR,
1348 "DW_DLE_ZLIB_UNCOMPRESS_ERROR"
1349 " The zstd compressed section is"
1350 " absurdly large given the input section"
1351 " length. So corrupt dwarf");
1352 return DW_DLV_ERROR;
1353 }
1354 }
1355 if ((src +srclen) > endsection) {
1356 _dwarf_error_string(dbg, error,
1357 DW_DLE_ZLIB_SECTION_SHORT,
1358 "DW_DLE_ZDEBUG_ZLIB_SECTION_SHORT"
1359 " The zstd or zlib compressed section is"
1360 " longer than the section"
1361 " length. So corrupt dwarf");
1362 return DW_DLV_ERROR;
1363 }
1364 destlen = uncompressed_len;
1365 dest = malloc(destlen);
1366 if (!dest) {
1367 _dwarf_error_string(dbg, error,
1368 DW_DLE_ALLOC_FAIL,
1369 "DW_DLE_ALLOC_FAIL"
1370 " The zstd or zlib uncompressed space"
1371 " malloc failed: out of memory");
1372 return DW_DLV_ERROR;
1373 }
1374 /* uncompress is a zlib function. */
1375 if (!zstdcompress) {
1376 int res = 0;
1377 uLongf dlen = destlen;
1378
1379 res = uncompress(dest,&dlen,src,srclen);
1380 if (res == Z_BUF_ERROR) {
1381 free(dest);
1382 DWARF_DBG_ERROR(dbg, DW_DLE_ZLIB_BUF_ERROR, DW_DLV_ERROR);
1383 } else if (res == Z_MEM_ERROR) {
1384 free(dest);
1385 DWARF_DBG_ERROR(dbg, DW_DLE_ALLOC_FAIL, DW_DLV_ERROR);
1386 } else if (res != Z_OK) {
1387 free(dest);
1388 /* Probably Z_DATA_ERROR. */
1389 DWARF_DBG_ERROR(dbg, DW_DLE_ZLIB_DATA_ERROR,
1390 DW_DLV_ERROR);
1391 }
1392 }
1393 if (zstdcompress) {
1394 size_t zsize =
1395 ZSTD_decompress(dest,destlen,src,srclen);
1396 if (zsize != destlen) {
1397 free(dest);
1398 _dwarf_error_string(dbg, error,
1399 DW_DLE_ZLIB_DATA_ERROR,
1400 "DW_DLE_ZLIB_DATA_ERROR"
1401 " The zstd ZSTD_decompress() failed.");
1402 return DW_DLV_ERROR;
1403 }
1404 }
1405 /* Z_OK */
1406 section->dss_data = dest;
1407 section->dss_size = destlen;
1408 section->dss_data_was_malloc = TRUE;
1409 section->dss_did_decompress = TRUE;
1410 return DW_DLV_OK;
1411 }
1412 #endif /* HAVE_ZLIB && HAVE_ZSTD */
1413
1414 /* Load the ELF section with the specified index and set its
1415 dss_data pointer to the memory where it was loaded. */
1416 int
1417 _dwarf_load_section(Dwarf_Debug dbg,
1418 struct Dwarf_Section_s *section,
1419 Dwarf_Error * error)
1420 {
1421 int res = DW_DLV_ERROR;
1422 int err = 0;
1423 struct Dwarf_Obj_Access_Interface_a_s *o = 0;
1424
1425 /* check to see if the section is already loaded */
1426 if (section->dss_data != NULL) {
1427 return DW_DLV_OK;
1428 }
1429 o = dbg->de_obj_file;
1430 /* There is an elf convention that section index 0
1431 is reserved, and that section is always empty.
1432 Non-elf object formats must honor
1433 that by ensuring that (when they
1434 assign numbers to 'sections' or
1435 'section-like-things') they never
1436 assign a real section section-number
1437 0 to dss_index.
1438
1439 There is also a convention for 'bss' that that section
1440 and its like sections have no data but do have a size.
1441 That is never true of DWARF sections */
1442 res = o->ai_methods->om_load_section(
1443 o->ai_object, section->dss_index,
1444 &section->dss_data, &err);
1445 if (res == DW_DLV_ERROR) {
1446 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
1447 }
1448 /* For PE and mach-o all section data was always
1449 malloc'd. We do not need to set dss_data_was_malloc
1450 though as the o->object data will eventually free
1451 the original section data.
1452 The first character of any o->object struct gives the type. */
1453
1454 if (res == DW_DLV_NO_ENTRY) {
1455 /* Gets this for section->dss_index 0.
1456 Which by ELF definition is a section index
1457 which is not used (reserved by Elf to
1458 mean no-section-index).
1459 Otherwise NULL dss_data gets error.
1460 BSS would legitimately have no data, but
1461 no DWARF related section could possibly be bss.
1462 We also get it if the section is present but
1463 zero-size. */
1464 return res;
1465 }
1466 if (section->dss_ignore_reloc_group_sec) {
1467 /* Neither zdebug nor reloc apply to .group sections. */
1468 return res;
1469 }
1470 if ((section->dss_zdebug_requires_decompress ||
1471 section->dss_shf_compressed ||
1472 section->dss_ZLIB_compressed) &&
1473 !section->dss_did_decompress) {
1474 if (!section->dss_data) {
1475 /* Impossible. This makes no sense.
1476 Corrupt object. */
1477 DWARF_DBG_ERROR(dbg, DW_DLE_COMPRESSED_EMPTY_SECTION,
1478 DW_DLV_ERROR);
1479 }
1480 #if defined(HAVE_ZLIB) && defined(HAVE_ZSTD)
1481 res = do_decompress(dbg,section,error);
1482 if (res != DW_DLV_OK) {
1483 return res;
1484 }
1485 #else /* !defined(HAVE_ZLIB) && defined(HAVE_ZSTD) */
1486 _dwarf_error_string(dbg, error,
1487 DW_DLE_ZDEBUG_REQUIRES_ZLIB,
1488 "DW_DLE_ZDEBUG_REQUIRES_ZLIB: "
1489 " zlib and zstd are missing, cannot"
1490 " decompress section.");
1491 return DW_DLV_ERROR;
1492 #endif /* defined(HAVE_ZLIB) && defined(HAVE_ZSTD) */
1493 section->dss_did_decompress = TRUE;
1494 }
1495 if (_dwarf_apply_relocs == 0) {
1496 return res;
1497 }
1498 if (section->dss_reloc_size == 0) {
1499 return res;
1500 }
1501 if (!o->ai_methods->om_relocate_a_section) {
1502 return res;
1503 }
1504 /*apply relocations */
1505 res = o->ai_methods->om_relocate_a_section(o->ai_object,
1506 section->dss_index, dbg, &err);
1507 if (res == DW_DLV_ERROR) {
1508 DWARF_DBG_ERROR(dbg, err, res);
1509 }
1510 return res;
1511 }
1512
1513 /* This is a hack so clients can verify offsets.
1514 Added (without so many sections to report) April 2005
1515 so that debugger can detect broken offsets
1516 (which happened in an IRIX -64 executable larger than 2GB
1517 using MIPSpro 7.3.1.3 compilers. A couple .debug_pubnames
1518 offsets were wrong.).
1519 */
1520 /* Now with sections new to DWARF5 */
1521 int
1522 dwarf_get_section_max_offsets_d(Dwarf_Debug dbg,
1523 Dwarf_Unsigned * debug_info_size,
1524 Dwarf_Unsigned * debug_abbrev_size,
1525 Dwarf_Unsigned * debug_line_size,
1526 Dwarf_Unsigned * debug_loc_size,
1527 Dwarf_Unsigned * debug_aranges_size,
1528 Dwarf_Unsigned * debug_macinfo_size,
1529 Dwarf_Unsigned * debug_pubnames_size,
1530 Dwarf_Unsigned * debug_str_size,
1531 Dwarf_Unsigned * debug_frame_size,
1532 Dwarf_Unsigned * debug_ranges_size,
1533 Dwarf_Unsigned * debug_typenames_size,
1534 Dwarf_Unsigned * debug_types_size,
1535 Dwarf_Unsigned * debug_macro_size,
1536 Dwarf_Unsigned * debug_str_offsets_size,
1537 Dwarf_Unsigned * debug_sup_size,
1538 Dwarf_Unsigned * debug_cu_index_size,
1539 Dwarf_Unsigned * debug_tu_index_size,
1540 Dwarf_Unsigned * debug_names_size,
1541 Dwarf_Unsigned * debug_loclists_size,
1542 Dwarf_Unsigned * debug_rnglists_size)
1543 {
1544 if (IS_INVALID_DBG(dbg)) {
1545 return DW_DLV_NO_ENTRY;
1546 }
1547 if (debug_info_size) {
1548 *debug_info_size = dbg->de_debug_info.dss_size;
1549 }
1550 if (debug_abbrev_size) {
1551 *debug_abbrev_size = dbg->de_debug_abbrev.dss_size;
1552 }
1553 if (debug_line_size) {
1554 *debug_line_size = dbg->de_debug_line.dss_size;
1555 }
1556 if (debug_loc_size) {
1557 *debug_loc_size = dbg->de_debug_loc.dss_size;
1558 }
1559 if (debug_aranges_size) {
1560 *debug_aranges_size = dbg->de_debug_aranges.dss_size;
1561 }
1562 if (debug_macinfo_size) {
1563 *debug_macinfo_size = dbg->de_debug_macinfo.dss_size;
1564 }
1565 if (debug_pubnames_size) {
1566 *debug_pubnames_size = dbg->de_debug_pubnames.dss_size;
1567 }
1568 if (debug_str_size) {
1569 *debug_str_size = dbg->de_debug_str.dss_size;
1570 }
1571 if (debug_frame_size) {
1572 *debug_frame_size = dbg->de_debug_frame.dss_size;
1573 }
1574 if (debug_ranges_size) {
1575 *debug_ranges_size = dbg->de_debug_ranges.dss_size;
1576 }
1577 if (debug_typenames_size) {
1578 *debug_typenames_size = dbg->de_debug_typenames.dss_size;
1579 }
1580 if (debug_types_size) {
1581 *debug_types_size = dbg->de_debug_types.dss_size;
1582 }
1583 if (debug_macro_size) {
1584 *debug_macro_size = dbg->de_debug_macro.dss_size;
1585 }
1586 if (debug_str_offsets_size) {
1587 *debug_str_offsets_size = dbg->de_debug_str_offsets.dss_size;
1588 }
1589 if (debug_sup_size) {
1590 *debug_sup_size = dbg->de_debug_sup.dss_size;
1591 }
1592 if (debug_cu_index_size) {
1593 *debug_cu_index_size = dbg->de_debug_cu_index.dss_size;
1594 }
1595 if (debug_tu_index_size) {
1596 *debug_tu_index_size = dbg->de_debug_tu_index.dss_size;
1597 }
1598 if (debug_names_size) {
1599 *debug_names_size = dbg->de_debug_names.dss_size;
1600 }
1601 if (debug_loclists_size) {
1602 *debug_loclists_size = dbg->de_debug_loclists.dss_size;
1603 }
1604 if (debug_rnglists_size) {
1605 *debug_rnglists_size = dbg->de_debug_rnglists.dss_size;
1606 }
1607 return DW_DLV_OK;
1608 }
1609
1610 const struct Dwarf_Obj_Access_Section_a_s zerodoas;
1611 /* Given a section name, get its size and address */
1612 int
1613 dwarf_get_section_info_by_name(Dwarf_Debug dbg,
1614 const char *section_name,
1615 Dwarf_Addr *section_addr,
1616 Dwarf_Unsigned *section_size,
1617 Dwarf_Error * error)
1618 {
1619 return dwarf_get_section_info_by_name_a(dbg,
1620 section_name,
1621 section_addr,
1622 section_size,
1623 0,0,
1624 error);
1625 }
1626 int
1627 dwarf_get_section_info_by_name_a(Dwarf_Debug dbg,
1628 const char *section_name,
1629 Dwarf_Addr *section_addr,
1630 Dwarf_Unsigned *section_size,
1631 Dwarf_Unsigned *section_flags,
1632 Dwarf_Unsigned *section_offset,
1633 Dwarf_Error * error)
1634 {
1635 struct Dwarf_Obj_Access_Interface_a_s * obj = 0;
1636 Dwarf_Unsigned section_count = 0;
1637 Dwarf_Unsigned section_index = 0;
1638 struct Dwarf_Obj_Access_Section_a_s doas;
1639
1640 CHECK_DBG(dbg,error,"dwarf_get_section_info_by_name_a()");
1641 if (section_addr) {
1642 *section_addr = 0;
1643 }
1644 if (section_size) {
1645 *section_size = 0;
1646 }
1647 if (section_flags) {
1648 *section_flags = 0;
1649 }
1650 if (section_offset) {
1651 *section_offset = 0;
1652 }
1653 if (!section_name) {
1654 _dwarf_error_string(dbg,error,DW_DLE_DBG_NULL,
1655 "DW_DLE_DBG_NULL: null section_name pointer "
1656 "passed to "
1657 "dwarf_get_section_info_by_name_a");
1658 return DW_DLV_ERROR;
1659 }
1660 if (!section_name[0]) {
1661 return DW_DLV_NO_ENTRY;
1662 }
1663 obj = dbg->de_obj_file;
1664 if (!obj) {
1665 return DW_DLV_NO_ENTRY;
1666 }
1667 section_count = obj->ai_methods->
1668 om_get_section_count(obj->ai_object);
1669
1670 /* We can skip index 0 when considering ELF files, but not other
1671 object types. */
1672 for (section_index = 0; section_index < section_count;
1673 ++section_index) {
1674 int errnum = 0;
1675 int res = 0;
1676
1677 doas = zerodoas;
1678 res = obj->ai_methods->
1679 om_get_section_info(obj->ai_object,
1680 section_index, &doas, &errnum);
1681 if (res == DW_DLV_ERROR) {
1682 DWARF_DBG_ERROR(dbg, errnum, DW_DLV_ERROR);
1683 }
1684 if (res == DW_DLV_NO_ENTRY) {
1685 /* This should be impossible */
1686 continue;
1687 }
1688 if (!strcmp(section_name,doas.as_name)) {
1689 if (section_addr) {
1690 *section_addr = doas.as_addr;
1691 }
1692 if (section_size) {
1693 *section_size = doas.as_size;
1694 }
1695 if (section_flags) {
1696 *section_flags = doas.as_flags;
1697 }
1698 if (section_offset) {
1699 *section_offset = doas.as_offset;
1700 }
1701 return DW_DLV_OK;
1702 }
1703 }
1704 return DW_DLV_NO_ENTRY;
1705 }
1706
1707 /* Given a section index, get its size and address */
1708 int
1709 dwarf_get_section_info_by_index(Dwarf_Debug dbg,
1710 int section_index,
1711 const char **section_name,
1712 Dwarf_Addr *section_addr,
1713 Dwarf_Unsigned *section_size,
1714 Dwarf_Error * error)
1715 {
1716 return dwarf_get_section_info_by_index_a(dbg,
1717 section_index,
1718 section_name,
1719 section_addr,
1720 section_size,
1721 0,0,
1722 error);
1723 }
1724 int
1725 dwarf_get_section_info_by_index_a(Dwarf_Debug dbg,
1726 int section_index,
1727 const char **section_name,
1728 Dwarf_Addr *section_addr,
1729 Dwarf_Unsigned *section_size,
1730 Dwarf_Unsigned *section_flags,
1731 Dwarf_Unsigned *section_offset,
1732 Dwarf_Error * error)
1733 {
1734 Dwarf_Unsigned sectioncount = 0;
1735 CHECK_DBG(dbg,error,"dwarf_get_section_info_by_index_a()");
1736
1737 sectioncount = dwarf_get_section_count(dbg);
1738
1739 if (section_addr) {
1740 *section_addr = 0;
1741 }
1742 if (section_size) {
1743 *section_size = 0;
1744 }
1745 if (section_name) {
1746 *section_name = 0;
1747 }
1748 if (section_flags) {
1749 *section_flags = 0;
1750 }
1751 if (section_offset) {
1752 *section_offset = 0;
1753 }
1754 if (section_index < 0) {
1755 return DW_DLV_NO_ENTRY;
1756 }
1757 /* Check if we have a valid section index */
1758 if ((Dwarf_Unsigned)section_index < sectioncount){
1759 int res = 0;
1760 int err = 0;
1761 struct Dwarf_Obj_Access_Section_a_s doas;
1762 struct Dwarf_Obj_Access_Interface_a_s * obj =
1763 dbg->de_obj_file;
1764 if (NULL == obj) {
1765 return DW_DLV_NO_ENTRY;
1766 }
1767 res = obj->ai_methods->om_get_section_info(obj->ai_object,
1768 section_index, &doas, &err);
1769 if (res == DW_DLV_ERROR){
1770 DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
1771 }
1772
1773 if (section_addr) {
1774 *section_addr = doas.as_addr;
1775 }
1776 if (section_size) {
1777 *section_size = doas.as_size;
1778 }
1779 if (section_name) {
1780 *section_name = doas.as_name;
1781 }
1782 if (section_flags) {
1783 *section_flags = doas.as_flags;
1784 }
1785 if (section_offset) {
1786 *section_offset = doas.as_offset;
1787 }
1788 return DW_DLV_OK;
1789 }
1790 return DW_DLV_NO_ENTRY;
1791 }
1792
1793 /* Get section count */
1794 Dwarf_Unsigned
1795 dwarf_get_section_count(Dwarf_Debug dbg)
1796 {
1797 struct Dwarf_Obj_Access_Interface_a_s * obj = 0;
1798
1799 if (IS_INVALID_DBG(dbg)) {
1800 return 0;
1801 }
1802 obj = dbg->de_obj_file;
1803 if (!obj) {
1804 /* -1 */
1805 return 0;
1806 }
1807 return obj->ai_methods->om_get_section_count(obj->ai_object);
1808 }
1809
1810 Dwarf_Cmdline_Options dwarf_cmdline_options = {
1811 FALSE /* Use quiet mode by default. */
1812 };
1813
1814 /* Lets libdwarf reflect a command line option, so we can get details
1815 of some errors printed using libdwarf-internal information. */
1816 void
1817 dwarf_record_cmdline_options(Dwarf_Cmdline_Options options)
1818 {
1819 dwarf_cmdline_options = options;
1820 }