Debian Codegraph @

src/lib/libdwarf/dwarf_setup_sections.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-2022 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_string.h"
48 #include "dwarf_secname_ck.h"
49 #include "dwarf_setup_sections.h"
50
51 /* Used to add the specific information for a debug related section
52 Called on each section of interest by section name.
53 DWARF_MAX_DEBUG_SECTIONS must be large enough to allow
54 that all sections of interest fit in the table.
55 returns DW_DLV_ERROR or DW_DLV_OK.
56 */
57 static int
58 add_debug_section_info(Dwarf_Debug dbg,
59 /* Name as seen in object file. */
60 const char *name,
61 const char *standard_section_name,
62 Dwarf_Unsigned obj_sec_num,
63 struct Dwarf_Section_s *secdata,
64 unsigned groupnum,
65 /* The have_dwarf flag is a somewhat imprecise
66 way to determine if there is at least one 'meaningful'
67 DWARF information section present in the object file.
68 If not set on some section we claim (later) that there
69 is no DWARF info present. see 'foundDwarf' in this file */
70 int duperr,int emptyerr,int have_dwarf,
71 int havezdebug,
72 int *err)
73 {
74 unsigned total_entries = dbg->de_debug_sections_total_entries;
75 if (secdata->dss_is_in_use) {
76 *err = duperr;
77 return DW_DLV_ERROR;
78 }
79 if (total_entries < DWARF_MAX_DEBUG_SECTIONS) {
80 struct Dwarf_dbg_sect_s *debug_section =
81 &dbg->de_debug_sections[total_entries];
82 secdata->dss_is_in_use = TRUE;
83 debug_section->ds_name = name;
84 debug_section->ds_number = obj_sec_num;
85 debug_section->ds_secdata = secdata;
86 debug_section->ds_groupnumber = groupnum;
87 secdata->dss_name = name; /* Actual name from object file. */
88 secdata->dss_standard_name = standard_section_name;
89 secdata->dss_number = obj_sec_num;
90 secdata->dss_zdebug_requires_decompress =
91 (Dwarf_Small)havezdebug;
92 /* We don't yet know about SHF_COMPRESSED */
93 debug_section->ds_duperr = duperr;
94 debug_section->ds_emptyerr = emptyerr;
95 debug_section->ds_have_dwarf = have_dwarf;
96 debug_section->ds_have_zdebug = havezdebug;
97 ++dbg->de_debug_sections_total_entries;
98 return DW_DLV_OK;
99 }
100 /* This represents a bug in libdwarf.
101 Mis-setup-DWARF_MAX_DEBUG_SECTIONS.
102 Or possibly a use of section groups that is
103 not supported. */
104 *err = DW_DLE_TOO_MANY_DEBUG;
105 return DW_DLV_ERROR;
106 }
107
108 /* Avoid adding offest to null s2.
109 This function avoids a compiler warning:
110 error: 'strcmp' reading 1 or more bytes
111 from a region of size 0
112 Offset is a fixed small positive number. */
113 static int
114 both_strings_nonempty(const char *s1, const char *s2, int offset)
115 {
116 const char *s3 = 0;
117 if (!s1 || !s2) {
118 return FALSE;
119 }
120 s3 = s2 + offset;
121 if (!s1[0] || !s3[0]) {
122 return FALSE;
123 }
124 return TRUE;
125 }
126
127 /* Return DW_DLV_OK etc.
128 PRECONDITION: secname and targname are non-null
129 pointers to strings. */
130 static int
131 set_up_section(Dwarf_Debug dbg,
132 /* Section name from object format.
133 Might start with .zdebug not .debug if compressed section. */
134 const char *secname,
135 /* Standard section name, such as .debug_info */
136 const char *sec_standard_name,
137 /* Section number from object format */
138 Dwarf_Unsigned obj_sec_num,
139 /* The name associated with this secdata in libdwarf */
140 const char *targname,
141 /* DW_GROUPNUMBER_ANY or BASE or DWO or some other group num */
142 unsigned groupnum_of_sec,
143 struct Dwarf_Section_s *secdata,
144 int duperr,int emptyerr,int have_dwarf,
145 int *err)
146 {
147 /* Here accommodate the .debug or .zdebug version, (and of
148 course non- .debug too, but those never zlib) .
149 SECNAMEMAX should be a little bigger than any section
150 name we care about as possibly compressed, which
151 is to say bigger than any standard section name. */
152 #define SECNAMEMAX 30
153 size_t secnamelen = strlen(secname);
154 /* static const char *dprefix = ".debug_"; */
155 #define DPREFIXLEN 7
156 static const char *zprefix = ".zdebug_";
157 #define ZPREFIXLEN 8
158 int havezdebug = FALSE;
159 int namesmatch = FALSE;
160 const char *postzprefix = 0;
161
162 /* For example, if the secname is .zdebug_info
163 we update the finaltargname to .debug_info
164 to match with the particular (known, predefined)
165 object section name.
166 We add one character, so check
167 to see if it will, in the end, fit.
168 See the SET_UP_SECTION macro. */
169
170 if (secnamelen >= SECNAMEMAX) {
171 /* This is not the target section.
172 our caller will keep looking. */
173 return DW_DLV_NO_ENTRY;
174 }
175 havezdebug = !strncmp(secname,zprefix,ZPREFIXLEN);
176 if (havezdebug) {
177 postzprefix = secname+ZPREFIXLEN;
178 }
179 /* With Alpine gcc
180 12.2.1_git20220924-r4) 12.2.1 20220924
181 and some other gcc versions when compiling
182 with -Werror and -fsanitize:
183 we get
184 error: 'strcmp' reading 1 or more bytes
185 from a region of size 0 [-Werror=stringop-overread]
186 So we add -Wnostringop-overread to the build as the error is
187 a false positive. We had to drop stringop-overread
188 references in compiler options, such turned off
189 valuable warnings. Oct 2024
190 refined the test to notice empty string */
191 if (both_strings_nonempty(postzprefix,targname,DPREFIXLEN) &&
192 !strcmp(postzprefix,targname+DPREFIXLEN)) {
193 /* zprefix version matches the object section
194 name so the section is compressed and is
195 the section this targname applies to. */
196 namesmatch = TRUE;
197 } else if (!strcmp(secname,targname)) {
198 namesmatch = TRUE;
199 } else { /* Fall to next statement */ }
200 #undef ZPREFIXLEN
201 #undef DPREFIXLEN
202 #undef SECNAMEMAX
203 if (!namesmatch) {
204 /* This is not the target section.
205 our caller will keep looking. */
206 return DW_DLV_NO_ENTRY;
207 }
208
209 /* SETUP_SECTION. See also BUILDING_SECTIONS, BUILDING_MAP */
210 {
211 /* The section name is a match with targname, or
212 the .zdebug version of targname. */
213 int sectionerr = 0;
214
215 sectionerr = add_debug_section_info(dbg,secname,
216 sec_standard_name,
217 obj_sec_num,
218 secdata,
219 groupnum_of_sec,
220 duperr,emptyerr, have_dwarf,
221 havezdebug,err);
222 if (sectionerr != DW_DLV_OK) {
223 /* *err is set already */
224 return sectionerr;
225 }
226 }
227 return DW_DLV_OK;
228 }
229
230 #define SET_UP_SECTION(mdbg,mname,mtarg,mgrp,minfo,me1,me2,mdw,mer) \
231 { \
232 int lerr = 0; \
233 lerr = set_up_section((mdbg), \
234 (mname), /* actual section name */ \
235 (mtarg), /* std section name */ \
236 /* scn_number from macro use context */ \
237 scn_number,(mtarg),(mgrp), \
238 (minfo), \
239 (me1),(me2),(mdw),(mer)); \
240 if (lerr != DW_DLV_NO_ENTRY) { \
241 return lerr; \
242 } /* else fall through. */ \
243 }
244
245 /* If running this long set of tests is slow
246 enough to matter one could set up a local
247 tsearch tree with all this content and search
248 it instead of this set of sequential tests.
249 Or use a switch(){} here with a search tree
250 to to turn name into index for the switch(). */
251 int
252 _dwarf_enter_section_in_de_debug_sections_array(Dwarf_Debug dbg,
253 const char *scn_name,
254 /* This is the number of the section in the object file. */
255 Dwarf_Unsigned scn_number,
256 unsigned group_number,
257 int *err)
258 {
259 /* Setup the table that contains the basic information about the
260 sections that are DWARF related. The entries are very unlikely
261 to change very often. */
262 SET_UP_SECTION(dbg,scn_name,".debug_info",
263 group_number,
264 &dbg->de_debug_info,
265 DW_DLE_DEBUG_INFO_DUPLICATE,DW_DLE_DEBUG_INFO_NULL,
266 TRUE,err);
267 SET_UP_SECTION(dbg,scn_name,".debug_info.dwo",
268 DW_GROUPNUMBER_DWO,
269 &dbg->de_debug_info,
270 DW_DLE_DEBUG_INFO_DUPLICATE,DW_DLE_DEBUG_INFO_NULL,
271 TRUE,err);
272 SET_UP_SECTION(dbg,scn_name,".debug_types",
273 group_number,
274 &dbg->de_debug_types,
275 DW_DLE_DEBUG_TYPES_DUPLICATE,DW_DLE_DEBUG_TYPES_NULL,
276 TRUE,err);
277 /* types.dwo is non-standard. DWARF4 GNU maybe. */
278 SET_UP_SECTION(dbg,scn_name,".debug_types.dwo",
279 DW_GROUPNUMBER_DWO,
280 &dbg->de_debug_types,
281 DW_DLE_DEBUG_TYPES_DUPLICATE,DW_DLE_DEBUG_TYPES_NULL,
282 TRUE,err);
283 SET_UP_SECTION(dbg,scn_name,".debug_abbrev",
284 group_number,
285 &dbg->de_debug_abbrev, /*03*/
286 DW_DLE_DEBUG_ABBREV_DUPLICATE,DW_DLE_DEBUG_ABBREV_NULL,
287 TRUE,err);
288 SET_UP_SECTION(dbg,scn_name,".debug_abbrev.dwo",
289 DW_GROUPNUMBER_DWO,
290 &dbg->de_debug_abbrev, /*03*/
291 DW_DLE_DEBUG_ABBREV_DUPLICATE,DW_DLE_DEBUG_ABBREV_NULL,
292 TRUE,err);
293 SET_UP_SECTION(dbg,scn_name,".debug_aranges",
294 group_number,
295 &dbg->de_debug_aranges,
296 DW_DLE_DEBUG_ARANGES_DUPLICATE,0,
297 FALSE,err);
298 SET_UP_SECTION(dbg,scn_name,".debug_line",
299 group_number,
300 &dbg->de_debug_line,
301 DW_DLE_DEBUG_LINE_DUPLICATE,0,
302 TRUE,err);
303 /* DWARF5 */
304 SET_UP_SECTION(dbg,scn_name,".debug_line_str",
305 group_number,
306 &dbg->de_debug_line_str,
307 DW_DLE_DEBUG_LINE_DUPLICATE,0,
308 FALSE,err);
309 SET_UP_SECTION(dbg,scn_name,".debug_line.dwo",
310 DW_GROUPNUMBER_DWO,
311 &dbg->de_debug_line,
312 DW_DLE_DEBUG_LINE_DUPLICATE,0,
313 TRUE,err);
314 SET_UP_SECTION(dbg,scn_name,".debug_frame",
315 group_number,
316 &dbg->de_debug_frame,
317 DW_DLE_DEBUG_FRAME_DUPLICATE,0,
318 TRUE,err);
319 /* gnu egcs-1.1.2 data */
320 SET_UP_SECTION(dbg,scn_name,".eh_frame",
321 group_number,
322 &dbg->de_debug_frame_eh_gnu,
323 DW_DLE_DEBUG_FRAME_DUPLICATE,0,
324 TRUE,err);
325 SET_UP_SECTION(dbg,scn_name,".debug_loc",
326 group_number,
327 &dbg->de_debug_loc,
328 DW_DLE_DEBUG_LOC_DUPLICATE,0,
329 FALSE,err);
330 /* .debug_loc.dwo would be non-standard. */
331 SET_UP_SECTION(dbg,scn_name,".debug_loc.dwo",
332 DW_GROUPNUMBER_DWO,
333 &dbg->de_debug_loc,
334 DW_DLE_DEBUG_LOC_DUPLICATE,0,
335 FALSE,err);
336 SET_UP_SECTION(dbg,scn_name,".debug_pubnames",
337 group_number,
338 &dbg->de_debug_pubnames,
339 DW_DLE_DEBUG_PUBNAMES_DUPLICATE,0,
340 FALSE,err);
341 SET_UP_SECTION(dbg,scn_name,".debug_str",
342 group_number,
343 &dbg->de_debug_str,
344 DW_DLE_DEBUG_STR_DUPLICATE,0,
345 FALSE,err);
346 SET_UP_SECTION(dbg,scn_name,".debug_str.dwo",
347 DW_GROUPNUMBER_DWO,
348 &dbg->de_debug_str,
349 DW_DLE_DEBUG_STR_DUPLICATE,0,
350 FALSE,err);
351 /* Section new in DWARF3. */
352 SET_UP_SECTION(dbg,scn_name,".debug_pubtypes",
353 group_number,
354 &dbg->de_debug_pubtypes,
355 /*13*/ DW_DLE_DEBUG_PUBTYPES_DUPLICATE,0,
356 FALSE,err);
357 /* DWARF5 */
358 SET_UP_SECTION(dbg,scn_name,".debug_loclists",
359 group_number,
360 &dbg->de_debug_loclists,
361 /*13*/ DW_DLE_DEBUG_LOClISTS_DUPLICATE,0,
362 FALSE,err);
363 /* DWARF5 */
364 SET_UP_SECTION(dbg,scn_name,".debug_loclists.dwo",
365 DW_GROUPNUMBER_DWO,
366 &dbg->de_debug_loclists,
367 /*13*/ DW_DLE_DEBUG_LOClISTS_DUPLICATE,0,
368 FALSE,err);
369 /* DWARF5 */
370 SET_UP_SECTION(dbg,scn_name,".debug_rnglists",
371 group_number,
372 &dbg->de_debug_rnglists,
373 /*13*/ DW_DLE_DEBUG_RNGLISTS_DUPLICATE,0,
374 FALSE,err);
375 /* DWARF5 */
376 SET_UP_SECTION(dbg,scn_name,".debug_rnglists.dwo",
377 DW_GROUPNUMBER_DWO,
378 &dbg->de_debug_rnglists,
379 /*13*/ DW_DLE_DEBUG_RNGLISTS_DUPLICATE,0,
380 FALSE,err);
381 /* DWARF5 */
382 SET_UP_SECTION(dbg,scn_name,".debug_str_offsets",
383 group_number,
384 &dbg->de_debug_str_offsets,
385 DW_DLE_DEBUG_STR_OFFSETS_DUPLICATE,0,
386 FALSE,err);
387 /* DWARF5 */
388 SET_UP_SECTION(dbg,scn_name,".debug_str_offsets.dwo",
389 DW_GROUPNUMBER_DWO,
390 &dbg->de_debug_str_offsets,
391 DW_DLE_DEBUG_STR_OFFSETS_DUPLICATE,0,
392 FALSE,err);
393
394 /* SGI IRIX-only. */
395 SET_UP_SECTION(dbg,scn_name,".debug_funcnames",
396 group_number,
397 &dbg->de_debug_funcnames,
398 /*11*/ DW_DLE_DEBUG_FUNCNAMES_DUPLICATE,0,
399 FALSE,err);
400 /* SGI IRIX-only, created years before DWARF3. Content
401 essentially identical to .debug_pubtypes. */
402 SET_UP_SECTION(dbg,scn_name,".debug_typenames",
403 group_number,
404 &dbg->de_debug_typenames,
405 /*12*/ DW_DLE_DEBUG_TYPENAMES_DUPLICATE,0,
406 FALSE,err);
407 /* SGI IRIX-only. */
408 SET_UP_SECTION(dbg,scn_name,".debug_varnames",
409 group_number,
410 &dbg->de_debug_varnames,
411 DW_DLE_DEBUG_VARNAMES_DUPLICATE,0,
412 FALSE,err);
413 /* SGI IRIX-only. */
414 SET_UP_SECTION(dbg,scn_name,".debug_weaknames",
415 group_number,
416 &dbg->de_debug_weaknames,
417 DW_DLE_DEBUG_WEAKNAMES_DUPLICATE,0,
418 FALSE,err);
419
420 SET_UP_SECTION(dbg,scn_name,".debug_macinfo",
421 group_number,
422 &dbg->de_debug_macinfo,
423 DW_DLE_DEBUG_MACINFO_DUPLICATE,0,
424 TRUE,err);
425 /* ".debug_macinfo.dwo" is not allowed. */
426
427 /* DWARF5 */
428 SET_UP_SECTION(dbg,scn_name,".debug_macro",
429 group_number,
430 &dbg->de_debug_macro,
431 DW_DLE_DEBUG_MACRO_DUPLICATE,0,
432 TRUE,err);
433 /* DWARF5 */
434 SET_UP_SECTION(dbg,scn_name,".debug_macro.dwo",
435 DW_GROUPNUMBER_DWO,
436 &dbg->de_debug_macro,
437 DW_DLE_DEBUG_MACRO_DUPLICATE,0,
438 TRUE,err);
439 SET_UP_SECTION(dbg,scn_name,".debug_ranges",
440 group_number,
441 &dbg->de_debug_ranges,
442 DW_DLE_DEBUG_RANGES_DUPLICATE,0,
443 TRUE,err);
444 /* No .debug_ranges.dwo allowed. */
445
446 /* New DWARF5 */
447 SET_UP_SECTION(dbg,scn_name,".debug_sup",
448 group_number,
449 &dbg->de_debug_sup,
450 DW_DLE_DEBUG_SUP_DUPLICATE,0,
451 TRUE,err);
452 /* No .debug_sup.dwo allowed. */
453
454 /* .symtab and .strtab have to be in any group. */
455 SET_UP_SECTION(dbg,scn_name,".symtab",
456 group_number,
457 &dbg->de_elf_symtab,
458 DW_DLE_DEBUG_SYMTAB_ERR,0,
459 FALSE,err);
460 SET_UP_SECTION(dbg,scn_name,".strtab",
461 group_number,
462 &dbg->de_elf_strtab,
463 DW_DLE_DEBUG_STRTAB_ERR,0,
464 FALSE,err);
465
466 /* New DWARF5 */
467 SET_UP_SECTION(dbg,scn_name,".debug_addr",
468 group_number,
469 &dbg->de_debug_addr,
470 DW_DLE_DEBUG_ADDR_DUPLICATE,0,
471 TRUE,err);
472 /* No .debug_addr.dwo allowed. */
473
474 /* gdb added this. */
475 SET_UP_SECTION(dbg,scn_name,".gdb_index",
476 group_number,
477 &dbg->de_debug_gdbindex,
478 DW_DLE_DUPLICATE_GDB_INDEX,0,
479 FALSE,err);
480
481 /* New DWARF5 */
482 SET_UP_SECTION(dbg,scn_name,".debug_names",
483 group_number,
484 &dbg->de_debug_names,
485 /*13*/ DW_DLE_DEBUG_NAMES_DUPLICATE,0,
486 FALSE,err);
487 /* No .debug_names.dwo allowed. */
488
489 /* gdb added this in DW4. It is in standard DWARF5 */
490 SET_UP_SECTION(dbg,scn_name,".debug_cu_index",
491 DW_GROUPNUMBER_DWO,
492 &dbg->de_debug_cu_index,
493 DW_DLE_DUPLICATE_CU_INDEX,0,
494 FALSE,err);
495 /* gdb added this in DW4. It is in standard DWARF5 */
496 SET_UP_SECTION(dbg,scn_name,".debug_tu_index",
497 DW_GROUPNUMBER_DWO,
498 &dbg->de_debug_tu_index,
499 DW_DLE_DUPLICATE_TU_INDEX,0,
500 FALSE,err);
501
502 /* GNU added this. It is not part of DWARF */
503 SET_UP_SECTION(dbg,scn_name,".gnu_debuglink",
504 DW_GROUPNUMBER_DWO,
505 &dbg->de_gnu_debuglink,
506 DW_DLE_DUPLICATE_GNU_DEBUGLINK,0,
507 FALSE,err);
508
509 /* GNU added this. It is not part of DWARF */
510 SET_UP_SECTION(dbg,scn_name,".note.gnu.build-id",
511 DW_GROUPNUMBER_DWO,
512 &dbg->de_note_gnu_buildid,
513 DW_DLE_DUPLICATE_GNU_DEBUGLINK,0,
514 FALSE,err);
515 /* GNU added this. It is not part of DWARF */
516 SET_UP_SECTION(dbg,scn_name,".debug_gnu_pubtypes.dwo",
517 DW_GROUPNUMBER_DWO,
518 &dbg->de_debug_gnu_pubtypes,
519 DW_DLE_DUPLICATE_GNU_DEBUG_PUBTYPES,0,
520 FALSE,err);
521 SET_UP_SECTION(dbg,scn_name,".debug_gnu_pubtypes",
522 group_number,
523 &dbg->de_debug_gnu_pubtypes,
524 DW_DLE_DUPLICATE_GNU_DEBUG_PUBTYPES,0,
525 FALSE,err);
526 SET_UP_SECTION(dbg,scn_name,".debug_gnu_pubnames.dwo",
527 DW_GROUPNUMBER_DWO,
528 &dbg->de_debug_gnu_pubnames,
529 DW_DLE_DUPLICATE_GNU_DEBUG_PUBNAMES,0,
530 FALSE,err);
531 SET_UP_SECTION(dbg,scn_name,".debug_gnu_pubnames",
532 group_number,
533 &dbg->de_debug_gnu_pubnames,
534 DW_DLE_DUPLICATE_GNU_DEBUG_PUBNAMES,0,
535 FALSE,err);
536 return DW_DLV_NO_ENTRY;
537 }