1da0c48c4Sopenharmony_ci/* Find the split (or skeleton) unit for a given unit. 2da0c48c4Sopenharmony_ci Copyright (C) 2018 Red Hat, Inc. 3da0c48c4Sopenharmony_ci This file is part of elfutils. 4da0c48c4Sopenharmony_ci 5da0c48c4Sopenharmony_ci This file is free software; you can redistribute it and/or modify 6da0c48c4Sopenharmony_ci it under the terms of either 7da0c48c4Sopenharmony_ci 8da0c48c4Sopenharmony_ci * the GNU Lesser General Public License as published by the Free 9da0c48c4Sopenharmony_ci Software Foundation; either version 3 of the License, or (at 10da0c48c4Sopenharmony_ci your option) any later version 11da0c48c4Sopenharmony_ci 12da0c48c4Sopenharmony_ci or 13da0c48c4Sopenharmony_ci 14da0c48c4Sopenharmony_ci * the GNU General Public License as published by the Free 15da0c48c4Sopenharmony_ci Software Foundation; either version 2 of the License, or (at 16da0c48c4Sopenharmony_ci your option) any later version 17da0c48c4Sopenharmony_ci 18da0c48c4Sopenharmony_ci or both in parallel, as here. 19da0c48c4Sopenharmony_ci 20da0c48c4Sopenharmony_ci elfutils is distributed in the hope that it will be useful, but 21da0c48c4Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 22da0c48c4Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23da0c48c4Sopenharmony_ci General Public License for more details. 24da0c48c4Sopenharmony_ci 25da0c48c4Sopenharmony_ci You should have received copies of the GNU General Public License and 26da0c48c4Sopenharmony_ci the GNU Lesser General Public License along with this program. If 27da0c48c4Sopenharmony_ci not, see <http://www.gnu.org/licenses/>. */ 28da0c48c4Sopenharmony_ci 29da0c48c4Sopenharmony_ci#ifdef HAVE_CONFIG_H 30da0c48c4Sopenharmony_ci# include <config.h> 31da0c48c4Sopenharmony_ci#endif 32da0c48c4Sopenharmony_ci 33da0c48c4Sopenharmony_ci#include "libdwP.h" 34da0c48c4Sopenharmony_ci#include "libelfP.h" 35da0c48c4Sopenharmony_ci 36da0c48c4Sopenharmony_ci#include <limits.h> 37da0c48c4Sopenharmony_ci#include <search.h> 38da0c48c4Sopenharmony_ci#include <stdlib.h> 39da0c48c4Sopenharmony_ci#include <string.h> 40da0c48c4Sopenharmony_ci#include <sys/types.h> 41da0c48c4Sopenharmony_ci#include <sys/stat.h> 42da0c48c4Sopenharmony_ci#include <fcntl.h> 43da0c48c4Sopenharmony_ci 44da0c48c4Sopenharmony_civoid 45da0c48c4Sopenharmony_citry_split_file (Dwarf_CU *cu, const char *dwo_path) 46da0c48c4Sopenharmony_ci{ 47da0c48c4Sopenharmony_ci int split_fd = open (dwo_path, O_RDONLY); 48da0c48c4Sopenharmony_ci if (split_fd != -1) 49da0c48c4Sopenharmony_ci { 50da0c48c4Sopenharmony_ci Dwarf *split_dwarf = dwarf_begin (split_fd, DWARF_C_READ); 51da0c48c4Sopenharmony_ci if (split_dwarf != NULL) 52da0c48c4Sopenharmony_ci { 53da0c48c4Sopenharmony_ci Dwarf_CU *split = NULL; 54da0c48c4Sopenharmony_ci while (dwarf_get_units (split_dwarf, split, &split, 55da0c48c4Sopenharmony_ci NULL, NULL, NULL, NULL) == 0) 56da0c48c4Sopenharmony_ci { 57da0c48c4Sopenharmony_ci if (split->unit_type == DW_UT_split_compile 58da0c48c4Sopenharmony_ci && cu->unit_id8 == split->unit_id8) 59da0c48c4Sopenharmony_ci { 60da0c48c4Sopenharmony_ci if (tsearch (split->dbg, &cu->dbg->split_tree, 61da0c48c4Sopenharmony_ci __libdw_finddbg_cb) == NULL) 62da0c48c4Sopenharmony_ci { 63da0c48c4Sopenharmony_ci /* Something went wrong. Don't link. */ 64da0c48c4Sopenharmony_ci __libdw_seterrno (DWARF_E_NOMEM); 65da0c48c4Sopenharmony_ci break; 66da0c48c4Sopenharmony_ci } 67da0c48c4Sopenharmony_ci 68da0c48c4Sopenharmony_ci /* Link skeleton and split compile units. */ 69da0c48c4Sopenharmony_ci __libdw_link_skel_split (cu, split); 70da0c48c4Sopenharmony_ci 71da0c48c4Sopenharmony_ci /* We have everything we need from this ELF 72da0c48c4Sopenharmony_ci file. And we are going to close the fd to 73da0c48c4Sopenharmony_ci not run out of file descriptors. */ 74da0c48c4Sopenharmony_ci elf_cntl (split_dwarf->elf, ELF_C_FDDONE); 75da0c48c4Sopenharmony_ci break; 76da0c48c4Sopenharmony_ci } 77da0c48c4Sopenharmony_ci } 78da0c48c4Sopenharmony_ci if (cu->split == (Dwarf_CU *) -1) 79da0c48c4Sopenharmony_ci dwarf_end (split_dwarf); 80da0c48c4Sopenharmony_ci } 81da0c48c4Sopenharmony_ci /* Always close, because we don't want to run out of file 82da0c48c4Sopenharmony_ci descriptors. See also the elf_fcntl ELF_C_FDDONE call 83da0c48c4Sopenharmony_ci above. */ 84da0c48c4Sopenharmony_ci close (split_fd); 85da0c48c4Sopenharmony_ci } 86da0c48c4Sopenharmony_ci} 87da0c48c4Sopenharmony_ci 88da0c48c4Sopenharmony_ciDwarf_CU * 89da0c48c4Sopenharmony_ciinternal_function 90da0c48c4Sopenharmony_ci__libdw_find_split_unit (Dwarf_CU *cu) 91da0c48c4Sopenharmony_ci{ 92da0c48c4Sopenharmony_ci /* Only try once. */ 93da0c48c4Sopenharmony_ci if (cu->split != (Dwarf_CU *) -1) 94da0c48c4Sopenharmony_ci return cu->split; 95da0c48c4Sopenharmony_ci 96da0c48c4Sopenharmony_ci /* We need a skeleton unit with a comp_dir and [GNU_]dwo_name attributes. 97da0c48c4Sopenharmony_ci The split unit will be the first in the dwo file and should have the 98da0c48c4Sopenharmony_ci same id as the skeleton. */ 99da0c48c4Sopenharmony_ci if (cu->unit_type == DW_UT_skeleton) 100da0c48c4Sopenharmony_ci { 101da0c48c4Sopenharmony_ci Dwarf_Die cudie = CUDIE (cu); 102da0c48c4Sopenharmony_ci Dwarf_Attribute dwo_name; 103da0c48c4Sopenharmony_ci /* It is fine if dwo_dir doesn't exists, but then dwo_name needs 104da0c48c4Sopenharmony_ci to be an absolute path. */ 105da0c48c4Sopenharmony_ci if (dwarf_attr (&cudie, DW_AT_dwo_name, &dwo_name) != NULL 106da0c48c4Sopenharmony_ci || dwarf_attr (&cudie, DW_AT_GNU_dwo_name, &dwo_name) != NULL) 107da0c48c4Sopenharmony_ci { 108da0c48c4Sopenharmony_ci /* First try the dwo file name in the same directory 109da0c48c4Sopenharmony_ci as we found the skeleton file. */ 110da0c48c4Sopenharmony_ci const char *dwo_file = dwarf_formstring (&dwo_name); 111da0c48c4Sopenharmony_ci const char *debugdir = cu->dbg->debugdir; 112da0c48c4Sopenharmony_ci char *dwo_path = __libdw_filepath (debugdir, NULL, dwo_file); 113da0c48c4Sopenharmony_ci if (dwo_path != NULL) 114da0c48c4Sopenharmony_ci { 115da0c48c4Sopenharmony_ci try_split_file (cu, dwo_path); 116da0c48c4Sopenharmony_ci free (dwo_path); 117da0c48c4Sopenharmony_ci } 118da0c48c4Sopenharmony_ci 119da0c48c4Sopenharmony_ci if (cu->split == (Dwarf_CU *) -1) 120da0c48c4Sopenharmony_ci { 121da0c48c4Sopenharmony_ci /* Try compdir plus dwo_name. */ 122da0c48c4Sopenharmony_ci Dwarf_Attribute compdir; 123da0c48c4Sopenharmony_ci dwarf_attr (&cudie, DW_AT_comp_dir, &compdir); 124da0c48c4Sopenharmony_ci const char *dwo_dir = dwarf_formstring (&compdir); 125da0c48c4Sopenharmony_ci if (dwo_dir != NULL) 126da0c48c4Sopenharmony_ci { 127da0c48c4Sopenharmony_ci dwo_path = __libdw_filepath (debugdir, dwo_dir, dwo_file); 128da0c48c4Sopenharmony_ci if (dwo_path != NULL) 129da0c48c4Sopenharmony_ci { 130da0c48c4Sopenharmony_ci try_split_file (cu, dwo_path); 131da0c48c4Sopenharmony_ci free (dwo_path); 132da0c48c4Sopenharmony_ci } 133da0c48c4Sopenharmony_ci } 134da0c48c4Sopenharmony_ci } 135da0c48c4Sopenharmony_ci /* XXX If still not found we could try stripping dirs from the 136da0c48c4Sopenharmony_ci comp_dir and adding them from the comp_dir, assuming 137da0c48c4Sopenharmony_ci someone moved a whole build tree around. */ 138da0c48c4Sopenharmony_ci } 139da0c48c4Sopenharmony_ci } 140da0c48c4Sopenharmony_ci 141da0c48c4Sopenharmony_ci /* If we found nothing, make sure we don't try again. */ 142da0c48c4Sopenharmony_ci if (cu->split == (Dwarf_CU *) -1) 143da0c48c4Sopenharmony_ci cu->split = NULL; 144da0c48c4Sopenharmony_ci 145da0c48c4Sopenharmony_ci return cu->split; 146da0c48c4Sopenharmony_ci} 147