Debian Codegraph @

src/lib/libdwarf/dwarf_generic_init.c

raw
1 /*
2 Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
4 Portions Copyright 2008-2010 Arxan Technologies, Inc. All rights reserved.
5 Portions Copyright 2011-2020 David Anderson. All rights reserved.
6 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
7
8 This program is free software; you can redistribute it
9 and/or modify it under the terms of version 2.1 of the
10 GNU Lesser General Public License as published by the Free
11 Software Foundation.
12
13 This program is distributed in the hope that it would be
14 useful, but WITHOUT ANY WARRANTY; without even the implied
15 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 PURPOSE.
17
18 Further, this software is distributed without any warranty
19 that it is free of the rightful claim of any third person
20 regarding infringement or the like. Any license provided
21 herein, whether implied or otherwise, applies only to this
22 software file. Patent licenses, if any, provided herein
23 do not apply to combinations of this program with other
24 software, or any other product whatsoever.
25
26 You should have received a copy of the GNU Lesser General
27 Public License along with this program; if not, write the
28 Free Software Foundation, Inc., 51 Franklin Street - Fifth
29 Floor, Boston MA 02110-1301, USA.
30
31 */
32 /*
33 Here is the deepest routes through dwarf_init_path_dl(),
34 depending on arguments.
35 It is called by dwarfdump to open an fd and return Dwarf_Debug.
36 Much of this is to handle GNU debuglink.
37 dwarf_init_path_dl(path true_path and globals, dbg1
38 dwarf_object_detector_path_dSYM (dsym only(
39 if returns DW_DLV_OK itis dSYM
40 dwarf_object_detector_path_b( &debuglink with global paths.
41 dwarf_object_detector_path_b ftype
42 check for dSYM if found it is the object to run on.
43 dwarf_object_detector_fd (gets size ftype)
44 return
45 _dwarf_debuglink_finder_internal(TRUE passing
46 in globals paths listr)
47 new local dbg
48 dwarf_init_path(path no dysm or debuglink
49 no global paths)
50 dwarf_object_detector_path_b( path no dsym
51 or debuglink no global paths
52 dwarf_object_detector (path
53 dwarf_object_detector_fd (gets size ftype)
54 for each global pathin list, add to dbg
55 dwarf_gnu_debuglink(dbg
56 for each global path in debuglink list
57 _dwarf_debuglink_finder_internal(FALSE
58 no global paths)
59 if crc match return OK with
60 pathname and fd returned
61 else return NO_ENTRY
62 */
63
64 #include <config.h>
65
66 #include <stddef.h> /* size_t */
67 #include <stdlib.h> /* free() */
68 #include <string.h> /* strdup() */
69 #include <stdio.h> /* debugging */
70
71 #include "dwarf.h"
72 #include "libdwarf.h"
73 #include "libdwarf_private.h"
74 #include "dwarf_base_types.h"
75 #include "dwarf_util.h"
76 #include "dwarf_opaque.h"
77 #include "dwarf_alloc.h"
78 #include "dwarf_error.h"
79 #include "dwarf_object_detector.h"
80
81 /* The design of Dwarf_Debug_s data on --file-tied
82 data and how it is used. See also dwarf_opaque.h
83 and dwarf_util.c
84
85 The fields involved are
86 de_dbg
87 de_primary_dbg
88 de_secondary_dbg
89 de_errors_dbg
90 de_tied_data.td_tied_object
91
92 On any init completing it will be considered
93 primary, Call it p1.
94 p1->de_dbg == p1
95 p1->de_primary_dbg == p1
96 p1->de_secondary_dbg == NULL
97 p1->de_errors_dbg == p1
98 p1->de_tied_data.td_tied_object = 0
99 Init a second object, call it p2 (settings as above).
100
101 Call dwarf_set_tied (p1,p2) (it is ok if p2 == NULL)
102 p1 is as above except that
103 p1->de_secondary_dbg == p2
104 p1->de_tied_data.td_tied_object = p2;
105 If p2 is non-null:
106 p2->de_dbg == p2
107 p2->de_primary_dbg = p1.
108 p2->de_secondary_dbg = p2
109 p2->de_errors_dbg = p1
110 All this is only useful if p1 has dwo/dwp sections
111 (split-dwarf) and p2 has the relevant TAG_skeleton(s)
112
113 If px->de_secondary_dbg is non-null
114 and px->secondary_dbg == px
115 then px is secondary.
116
117 If x->de_secondary_dbg is non-null
118 and px->secondary_dbg != px
119 then px is primary.
120
121 If px->de_secondary_dbg is null
122 then px is a primary. and there
123 is no secondary.
124
125 Call dwarf_set_tied(p1,NULL) and both p1 and
126 p2 are returned to initial conditions
127 as before they were tied together. */
128
129 static int
130 set_global_paths_init(Dwarf_Debug dbg, Dwarf_Error* error)
131 {
132 int res = 0;
133
134 res = dwarf_add_debuglink_global_path(dbg,
135 "/usr/lib/debug",error);
136 return res;
137 }
138
139 /* New in September 2023. */
140 int dwarf_init_path_a(const char *path,
141 char * true_path_out_buffer,
142 unsigned true_path_bufferlen,
143 unsigned groupnumber,
144 unsigned universalnumber,
145 Dwarf_Handler errhand,
146 Dwarf_Ptr errarg,
147 Dwarf_Debug * ret_dbg,
148 Dwarf_Error * error)
149 {
150 return dwarf_init_path_dl_a(path,
151 true_path_out_buffer,true_path_bufferlen,
152 groupnumber,universalnumber,
153 errhand,errarg,ret_dbg,
154 0,0,0,
155 error);
156 }
157
158 int dwarf_init_path(const char *path,
159 char * true_path_out_buffer,
160 unsigned true_path_bufferlen,
161 unsigned groupnumber,
162 Dwarf_Handler errhand,
163 Dwarf_Ptr errarg,
164 Dwarf_Debug * ret_dbg,
165 Dwarf_Error * error)
166 {
167 unsigned int universalnumber = 0;
168 return dwarf_init_path_dl_a(path,
169 true_path_out_buffer,true_path_bufferlen,
170 groupnumber,universalnumber,
171 errhand,errarg,ret_dbg,
172 0,0,0,
173 error);
174 }
175
176 static void
177 final_common_settings(Dwarf_Debug dbg,
178 const char *file_path,
179 int fd,
180 unsigned char lpath_source,
181 unsigned char *path_source,
182 Dwarf_Error *error)
183 {
184 int res = 0;
185
186 dbg->de_path = strdup(file_path);
187 dbg->de_fd = fd;
188 dbg->de_owns_fd = TRUE;
189 dbg->de_path_source = lpath_source;
190 if (path_source) {
191 *path_source = lpath_source;
192 }
193 dbg->de_owns_fd = TRUE;
194 res = set_global_paths_init(dbg,error);
195 if (res == DW_DLV_ERROR && error) {
196 dwarf_dealloc_error(dbg,*error);
197 *error = 0;
198 }
199 return;
200 }
201 /* New October 2020
202 Given true_path_out_buffer (and true_path_bufferlen)
203 non-zero this finds a dSYM (if such exists) with the
204 file name in true_path_out_buffer
205
206 If not a dSYM it follows debuglink rules to try to find a file
207 that matches requirements. If found returns DW_DLV_OK and
208 copies the name to true_path_out_buffer;
209 If none of the above found, it copies path into true_path
210 and returns DW_DLV_OK, we know the name is good;
211
212 The pathn_fd is owned by libdwarf and is in the created dbg->de_fd
213 field.
214 */
215 int
216 dwarf_init_path_dl(const char *path,
217 char * true_path_out_buffer,
218 unsigned true_path_bufferlen,
219 unsigned groupnumber,
220 Dwarf_Handler errhand,
221 Dwarf_Ptr errarg,
222 Dwarf_Debug * ret_dbg,
223 char ** dl_path_array,
224 unsigned int dl_path_count,
225 unsigned char * path_source,
226 Dwarf_Error * error)
227 {
228 unsigned int universalnumber = 0;
229 int res = 0;
230
231 res = dwarf_init_path_dl_a(path,
232 true_path_out_buffer, true_path_bufferlen,
233 groupnumber,universalnumber,
234 errhand,errarg,ret_dbg, dl_path_array,
235 dl_path_count,path_source,error);
236 return res;
237 }
238
239 #if 0 /* for debugging */
240 static void
241 dump_header_fields(const char *w,Dwarf_Debug dbg)
242 {
243 printf("Dumping certain fields of %s\n",w);
244 printf("ftype : %d\n",dbg->de_ftype);
245 printf("machine : %llu\n",dbg->de_obj_machine);
246 printf("flags : 0x%llx\n",dbg->de_obj_flags);
247 printf("pointer size : %u\n",dbg->de_pointer_size);
248 printf("big_endian? : %u\n",dbg->de_big_endian_object);
249 printf("ubcount : %u\n",dbg->de_universalbinary_count);
250 printf("ubindex : %u\n",dbg->de_universalbinary_index);
251 printf("ub offset : %llu\n",dbg->de_obj_ub_offset);
252 printf("path source : %u\n",dbg->de_path_source);
253 printf("comdat group# : %u\n",dbg->de_groupnumber);
254 exit(0);
255 }
256 #endif
257
258 int
259 dwarf_init_path_dl_a(const char *path,
260 char * true_path_out_buffer,
261 unsigned true_path_bufferlen,
262 unsigned groupnumber,
263 unsigned universalnumber,
264 Dwarf_Handler errhand,
265 Dwarf_Ptr errarg,
266 Dwarf_Debug * ret_dbg,
267 char ** dl_path_array,
268 unsigned int dl_path_count,
269 unsigned char * path_source,
270 Dwarf_Error * error)
271 {
272 unsigned ftype = 0;
273 unsigned endian = 0;
274 unsigned offsetsize = 0;
275 Dwarf_Unsigned filesize = 0;
276 int res = DW_DLV_ERROR;
277 int errcode = 0;
278 int fd = -1;
279 Dwarf_Debug dbg = 0;
280 char *file_path = 0;
281 unsigned char lpath_source = DW_PATHSOURCE_basic;
282
283 if (!ret_dbg) {
284 DWARF_DBG_ERROR(NULL,DW_DLE_DWARF_INIT_DBG_NULL,
285 DW_DLV_ERROR);
286 }
287 /* Non-null *ret_dbg will cause problems dealing with
288 DW_DLV_ERROR */
289 *ret_dbg = 0;
290 if (!path) {
291 /* Oops. Null path */
292 _dwarf_error_string(NULL,
293 error,DW_DLE_STRING_PTR_NULL,
294 "DW_DLE_STRING_PTR_NULL: Passing a"
295 " null path argument to "
296 "dwarf_init_path or dwarf_init_path_dl"
297 " cannot work. Error.");
298 return DW_DLV_ERROR;
299 }
300 /* a special dsym call so we only check once. */
301 if (true_path_out_buffer) {
302 res = dwarf_object_detector_path_dSYM(path,
303 true_path_out_buffer,
304 true_path_bufferlen,
305 dl_path_array,dl_path_count,
306 &ftype,&endian,&offsetsize,&filesize,
307 &lpath_source,
308 &errcode);
309 if (res != DW_DLV_OK) {
310 if (res == DW_DLV_ERROR) {
311 /* ignore error. Look further. */
312 errcode = 0;
313 }
314 }
315 }
316 if (res != DW_DLV_OK) {
317 res = dwarf_object_detector_path_b(path,
318 true_path_out_buffer,
319 true_path_bufferlen,
320 dl_path_array,dl_path_count,
321 &ftype,&endian,&offsetsize,&filesize,
322 &lpath_source,
323 &errcode);
324 if (res != DW_DLV_OK ) {
325 if (res == DW_DLV_ERROR) {
326 errcode = 0;
327 }
328 }
329 }
330 if (res != DW_DLV_OK) {
331 /* So as a last resort in case
332 of data corruption in the object.
333 Lets try without
334 investigating debuglink or dSYM. */
335 res = dwarf_object_detector_path_b(path,
336 0,
337 0,
338 dl_path_array,dl_path_count,
339 &ftype,&endian,&offsetsize,&filesize,
340 &lpath_source,
341 &errcode);
342 }
343 if (res != DW_DLV_OK) {
344 /* impossible. The last above *had* to work */
345 if (res == DW_DLV_ERROR) {
346 _dwarf_error(NULL, error, errcode);
347 }
348 return res;
349 }
350 /* ASSERT: lpath_source != DW_PATHSOURCE_unspecified */
351 if (lpath_source != DW_PATHSOURCE_basic &&
352 true_path_out_buffer && *true_path_out_buffer) {
353 /* MacOS dSYM or GNU debuglink */
354 file_path = true_path_out_buffer;
355 fd = _dwarf_openr(true_path_out_buffer);
356 } else {
357 /* ASSERT: lpath_source = DW_PATHSOURCE_basic */
358 file_path = (char *)path;
359 fd = _dwarf_openr(path);
360 }
361
362 if (fd == -1) {
363 DWARF_DBG_ERROR(NULL, DW_DLE_FILE_UNAVAILABLE,
364 DW_DLV_ERROR);
365 }
366 switch(ftype) {
367 case DW_FTYPE_ELF: {
368 res = _dwarf_elf_nlsetup(fd,
369 file_path,
370 ftype,endian,offsetsize,filesize,
371 groupnumber,errhand,errarg,&dbg,error);
372 if (res != DW_DLV_OK) {
373 _dwarf_closer(fd);
374 return res;
375 }
376 final_common_settings(dbg,file_path,fd,
377 lpath_source,path_source,error);
378 dbg->de_ftype = (Dwarf_Small)ftype;
379 *ret_dbg = dbg;
380 return res;
381 }
382 case DW_FTYPE_APPLEUNIVERSAL:
383 case DW_FTYPE_MACH_O: {
384 res = _dwarf_macho_setup(fd,
385 file_path,
386 universalnumber,
387 ftype,endian,offsetsize,filesize,
388 groupnumber,errhand,errarg,&dbg,error);
389 if (res != DW_DLV_OK) {
390 _dwarf_closer(fd);
391 return res;
392 }
393 final_common_settings(dbg,file_path,fd,
394 lpath_source,path_source,error);
395 dbg->de_ftype = (Dwarf_Small)ftype;
396 *ret_dbg = dbg;
397 return res;
398 }
399 case DW_FTYPE_PE: {
400 res = _dwarf_pe_setup(fd,
401 file_path,
402 ftype,endian,offsetsize,filesize,
403 groupnumber,errhand,errarg,&dbg,error);
404 if (res != DW_DLV_OK) {
405 _dwarf_closer(fd);
406 return res;
407 }
408 final_common_settings(dbg,file_path,fd,
409 lpath_source,path_source,error);
410 dbg->de_ftype = (Dwarf_Small)ftype;
411 *ret_dbg = dbg;
412 return res;
413 }
414 default:
415 _dwarf_closer(fd);
416 DWARF_DBG_ERROR(NULL, DW_DLE_FILE_WRONG_TYPE,
417 DW_DLV_ERROR);
418 /* Macro returns, cannot reach this line. */
419 }
420 /* Cannot reach this line */
421 }
422
423 /* New March 2017, this provides for reading
424 object files with multiple elf section groups.
425 If you are unsure about group_number, use
426 DW_GROUPNUMBER_ANY as groupnumber.
427 */
428 int
429 dwarf_init_b(int fd,
430 unsigned group_number,
431 Dwarf_Handler errhand,
432 Dwarf_Ptr errarg,
433 Dwarf_Debug * ret_dbg,
434 Dwarf_Error * error)
435 {
436 unsigned ftype = 0;
437 unsigned endian = 0;
438 unsigned offsetsize = 0;
439 unsigned universalnumber = 0;
440 Dwarf_Unsigned filesize = 0;
441 int res = 0;
442 int errcode = 0;
443
444 if (!ret_dbg) {
445 DWARF_DBG_ERROR(NULL,DW_DLE_DWARF_INIT_DBG_NULL,DW_DLV_ERROR);
446 }
447 /* Non-null *ret_dbg will cause problems dealing with
448 DW_DLV_ERROR */
449 *ret_dbg = 0;
450 res = dwarf_object_detector_fd(fd, &ftype,
451 &endian,&offsetsize,&filesize,&errcode);
452 if (res == DW_DLV_NO_ENTRY) {
453 return res;
454 }
455 if (res == DW_DLV_ERROR) {
456 /* This macro does a return. */
457 DWARF_DBG_ERROR(NULL, DW_DLE_FILE_WRONG_TYPE, DW_DLV_ERROR);
458 }
459 switch(ftype) {
460 case DW_FTYPE_ELF: {
461 int res2 = 0;
462
463 res2 = _dwarf_elf_nlsetup(fd,"",
464 ftype,endian,offsetsize,filesize,
465 group_number,errhand,errarg,ret_dbg,error);
466 if (res2 != DW_DLV_OK) {
467 return res2;
468 }
469 set_global_paths_init(*ret_dbg,error);
470 return res2;
471 }
472 case DW_FTYPE_APPLEUNIVERSAL:
473 case DW_FTYPE_MACH_O: {
474 int resm = 0;
475
476 resm = _dwarf_macho_setup(fd,"",
477 universalnumber,
478 ftype,endian,offsetsize,filesize,
479 group_number,errhand,errarg,ret_dbg,error);
480 if (resm != DW_DLV_OK) {
481 return resm;
482 }
483 set_global_paths_init(*ret_dbg,error);
484 return resm;
485 }
486
487 case DW_FTYPE_PE: {
488 int resp = 0;
489
490 resp = _dwarf_pe_setup(fd,
491 "",
492 ftype,endian,offsetsize,filesize,
493 group_number,errhand,errarg,ret_dbg,error);
494 if (resp != DW_DLV_OK) {
495 return resp;
496 }
497 set_global_paths_init(*ret_dbg,error);
498 return resp;
499 }
500 default: break;
501 }
502 DWARF_DBG_ERROR(NULL, DW_DLE_FILE_WRONG_TYPE, DW_DLV_ERROR);
503 /* Macro above returns. cannot reach here. */
504 }
505
506 /*
507 Frees all memory that was not previously freed
508 by dwarf_dealloc.
509 Aside from certain categories.
510
511 Applicable when dwarf_init() or dwarf_elf_init()
512 or the -b() form was used to init 'dbg'.
513 */
514 int
515 dwarf_finish(Dwarf_Debug dbg)
516 {
517 if (IS_INVALID_DBG(dbg)) {
518 _dwarf_free_static_errlist();
519 return DW_DLV_OK;
520 }
521 if (dbg->de_obj_file) {
522 /* The initial character of a valid
523 dbg->de_obj_file->object struct is a letter:
524 E, F, M, or P */
525 char otype = *(char *)(dbg->de_obj_file->ai_object);
526
527 switch(otype) {
528 case 'E':
529 break;
530 case 'F':
531 /* Non-libelf elf access */
532 _dwarf_destruct_elf_nlaccess(dbg->de_obj_file);
533 break;
534 case 'M':
535 _dwarf_destruct_macho_access(dbg->de_obj_file);
536 break;
537 case 'P':
538 _dwarf_destruct_pe_access(dbg->de_obj_file);
539 break;
540 default:
541 /* Do nothing. A serious internal error */
542 break;
543 }
544 }
545 if (dbg->de_owns_fd) {
546 _dwarf_closer(dbg->de_fd);
547 dbg->de_owns_fd = FALSE;
548 }
549 free((void *)dbg->de_path);
550 dbg->de_path = 0;
551 /* dwarf_object_finish() also frees de_path,
552 but that is safe because we set it to zero
553 here so no duplicate free will occur.
554 It never returns DW_DLV_ERROR.
555 Not all code uses libdwarf exactly as we do
556 hence the free() there. */
557 return dwarf_object_finish(dbg);
558 }
559
560 /*
561 tieddbg should be the executable or .o
562 that has the .debug_addr section that
563 the base dbg refers to. See Split Objects in DWARF5.
564 Or in DWARF5 maybe .debug_rnglists or .debug_loclists.
565
566 Allows calling with NULL though we really just set
567 primary_dbg->ge_primary to de_primary_dbg, thus cutting
568 links between main and any previous tied-file setup.
569 New September 2015.
570 Logic revised Nov 2024. See dwarf_opaque.h
571 */
572 int
573 dwarf_set_tied_dbg(Dwarf_Debug primary_dbg,
574 Dwarf_Debug secondary_dbg,
575 Dwarf_Error*error)
576 {
577 CHECK_DBG(primary_dbg,error,"dwarf_set_tied_dbg()");
578 if (secondary_dbg == primary_dbg) {
579 _dwarf_error_string(primary_dbg,error,
580 DW_DLE_NO_TIED_FILE_AVAILABLE,
581 "DW_DLE_NO_TIED_FILE_AVAILABLE: bad argument to "
582 "dwarf_set_tied_dbg(), tied and main must not be the "
583 "same pointer!");
584 return DW_DLV_ERROR;
585 }
586 if (secondary_dbg) {
587 if (primary_dbg->de_secondary_dbg ) {
588 _dwarf_error_string(primary_dbg,error,
589 DW_DLE_NO_TIED_FILE_AVAILABLE,
590 "DW_DLE_NO_TIED_FILE_AVAILABLE: bad argument to "
591 "dwarf_set_tied_dbg(), primary_dbg already has"
592 " a secondary_dbg!");
593 return DW_DLV_ERROR;
594 }
595 primary_dbg->de_tied_data.td_tied_object = secondary_dbg;
596 primary_dbg->de_secondary_dbg = secondary_dbg;
597 secondary_dbg->de_secondary_dbg = secondary_dbg;
598 secondary_dbg->de_errors_dbg = primary_dbg;
599 CHECK_DBG(secondary_dbg,error,"dwarf_set_tied_dbg() "
600 "dw_secondary_dbg"
601 "is invalid");
602 primary_dbg->de_secondary_dbg = secondary_dbg;
603 return DW_DLV_OK;
604 } else {
605 primary_dbg->de_secondary_dbg = 0;
606 primary_dbg->de_tied_data.td_tied_object = 0;
607 }
608 return DW_DLV_OK;
609 }
610
611 /* New September 2015.
612 As of Aug 2023 this correctly returns tied_dbg
613 whether main or tied passed in. Before this
614 it would return the dbg passed in.
615 If there is no tied-dbg this returns main dbg. */
616 int
617 dwarf_get_tied_dbg(Dwarf_Debug dw_dbg,
618 Dwarf_Debug *dw_secondary_dbg_out,
619 Dwarf_Error *dw_error)
620 {
621 CHECK_DBG(dw_dbg,dw_error,"dwarf_get_tied_dbg()");
622 *dw_secondary_dbg_out = 0;
623 if (DBG_IS_PRIMARY(dw_dbg)) {
624 if (!dw_dbg->de_secondary_dbg) {
625 *dw_secondary_dbg_out = dw_dbg;
626 return DW_DLV_OK;
627 }
628 *dw_secondary_dbg_out = dw_dbg->de_secondary_dbg;
629 return DW_DLV_OK;
630 }
631 if (DBG_IS_SECONDARY(dw_dbg)) {
632 *dw_secondary_dbg_out = dw_dbg;
633 return DW_DLV_OK;
634 }
635 /* Leave returned secondary_dbg_out NULL,
636 this should not happen */
637 return DW_DLV_OK;
638 }