1/* Create descriptor from ELF descriptor for processing file. 2 Copyright (C) 2002, 2003, 2004, 2005, 2007 Red Hat, Inc. 3 This file is part of Red Hat elfutils. 4 Written by Ulrich Drepper <drepper@redhat.com>, 2002. 5 6 Red Hat elfutils is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by the 8 Free Software Foundation; version 2 of the License. 9 10 Red Hat elfutils is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along 16 with Red Hat elfutils; if not, write to the Free Software Foundation, 17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. 18 19 In addition, as a special exception, Red Hat, Inc. gives You the 20 additional right to link the code of Red Hat elfutils with code licensed 21 under any Open Source Initiative certified open source license 22 (http://www.opensource.org/licenses/index.php) which requires the 23 distribution of source code with any binary distribution and to 24 distribute linked combinations of the two. Non-GPL Code permitted under 25 this exception must only link to the code of Red Hat elfutils through 26 those well defined interfaces identified in the file named EXCEPTION 27 found in the source code files (the "Approved Interfaces"). The files 28 of Non-GPL Code may instantiate templates or use macros or inline 29 functions from the Approved Interfaces without causing the resulting 30 work to be covered by the GNU General Public License. Only Red Hat, 31 Inc. may make changes or additions to the list of Approved Interfaces. 32 Red Hat's grant of this exception is conditioned upon your not adding 33 any new exceptions. If you wish to add a new Approved Interface or 34 exception, please contact Red Hat. You must obey the GNU General Public 35 License in all respects for all of the Red Hat elfutils code and other 36 code used in conjunction with Red Hat elfutils except the Non-GPL Code 37 covered by this exception. If you modify this file, you may extend this 38 exception to your version of the file, but you are not obligated to do 39 so. If you do not wish to provide this exception without modification, 40 you must delete this exception statement from your version and license 41 this file solely under the GPL without exception. 42 43 Red Hat elfutils is an included package of the Open Invention Network. 44 An included package of the Open Invention Network is a package for which 45 Open Invention Network licensees cross-license their patents. No patent 46 license is granted, either expressly or impliedly, by designation as an 47 included package. Should you wish to participate in the Open Invention 48 Network licensing program, please visit www.openinventionnetwork.com 49 <http://www.openinventionnetwork.com>. */ 50 51#ifdef HAVE_CONFIG_H 52# include <config.h> 53#endif 54 55#include <stdbool.h> 56#include <stddef.h> 57#include <stdlib.h> 58#include <string.h> 59#include <unistd.h> 60#include <sys/stat.h> 61 62#include "libdwP.h" 63 64 65/* Section names. */ 66static const char dwarf_scnnames[IDX_last][17] = 67{ 68 [IDX_debug_info] = ".debug_info", 69 [IDX_debug_abbrev] = ".debug_abbrev", 70 [IDX_debug_aranges] = ".debug_aranges", 71 [IDX_debug_line] = ".debug_line", 72 [IDX_debug_frame] = ".debug_frame", 73 [IDX_eh_frame] = ".eh_frame", 74 [IDX_debug_loc] = ".debug_loc", 75 [IDX_debug_pubnames] = ".debug_pubnames", 76 [IDX_debug_str] = ".debug_str", 77 [IDX_debug_funcnames] = ".debug_funcnames", 78 [IDX_debug_typenames] = ".debug_typenames", 79 [IDX_debug_varnames] = ".debug_varnames", 80 [IDX_debug_weaknames] = ".debug_weaknames", 81 [IDX_debug_macinfo] = ".debug_macinfo", 82 [IDX_debug_ranges] = ".debug_ranges" 83}; 84#define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0])) 85 86 87static Dwarf * 88check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp) 89{ 90 GElf_Shdr shdr_mem; 91 GElf_Shdr *shdr; 92 93 /* Get the section header data. */ 94 shdr = gelf_getshdr (scn, &shdr_mem); 95 if (shdr == NULL) 96 /* This should never happen. If it does something is 97 wrong in the libelf library. */ 98 abort (); 99 100 /* Ignore any SHT_NOBITS sections. Debugging sections should not 101 have been stripped, but in case of a corrupt file we won't try 102 to look at the missing data. */ 103 if (unlikely (shdr->sh_type == SHT_NOBITS)) 104 return result; 105 106 /* Make sure the section is part of a section group only iff we 107 really need it. If we are looking for the global (= non-section 108 group debug info) we have to ignore all the info in section 109 groups. If we are looking into a section group we cannot look at 110 a section which isn't part of the section group. */ 111 if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0) 112 /* Ignore the section. */ 113 return result; 114 115 116 /* We recognize the DWARF section by their names. This is not very 117 safe and stable but the best we can do. */ 118 const char *scnname = elf_strptr (result->elf, ehdr->e_shstrndx, 119 shdr->sh_name); 120 if (scnname == NULL) 121 { 122 /* The section name must be valid. Otherwise is the ELF file 123 invalid. */ 124 __libdw_seterrno (DWARF_E_INVALID_ELF); 125 free (result); 126 return NULL; 127 } 128 129 130 /* Recognize the various sections. Most names start with .debug_. */ 131 size_t cnt; 132 for (cnt = 0; cnt < ndwarf_scnnames; ++cnt) 133 if (strcmp (scnname, dwarf_scnnames[cnt]) == 0) 134 { 135 /* Found it. Remember where the data is. */ 136 if (unlikely (result->sectiondata[cnt] != NULL)) 137 /* A section appears twice. That's bad. We ignore the section. */ 138 break; 139 140 /* Get the section data. */ 141 Elf_Data *data = elf_getdata (scn, NULL); 142 if (data != NULL && data->d_size != 0) 143 /* Yep, there is actually data available. */ 144 result->sectiondata[cnt] = data; 145 146 break; 147 } 148 149 return result; 150} 151 152 153/* Check whether all the necessary DWARF information is available. */ 154static Dwarf * 155valid_p (Dwarf *result) 156{ 157 /* We looked at all the sections. Now determine whether all the 158 sections with debugging information we need are there. 159 160 XXX Which sections are absolutely necessary? Add tests if 161 necessary. For now we require only .debug_info. Hopefully this 162 is correct. */ 163 if (likely (result != NULL) 164 && unlikely (result->sectiondata[IDX_debug_info] == NULL)) 165 { 166 __libdw_seterrno (DWARF_E_NO_DWARF); 167 free (result); 168 result = NULL; 169 } 170 171 return result; 172} 173 174 175static Dwarf * 176global_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr) 177{ 178 Elf_Scn *scn = NULL; 179 180 while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL) 181 result = check_section (result, ehdr, scn, false); 182 183 return valid_p (result); 184} 185 186 187static Dwarf * 188scngrp_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr, Elf_Scn *scngrp) 189{ 190 /* SCNGRP is the section descriptor for a section group which might 191 contain debug sections. */ 192 Elf_Data *data = elf_getdata (scngrp, NULL); 193 if (data == NULL) 194 { 195 /* We cannot read the section content. Fail! */ 196 free (result); 197 return NULL; 198 } 199 200 /* The content of the section is a number of 32-bit words which 201 represent section indices. The first word is a flag word. */ 202 Elf32_Word *scnidx = (Elf32_Word *) data->d_buf; 203 size_t cnt; 204 for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt) 205 { 206 Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]); 207 if (scn == NULL) 208 { 209 /* A section group refers to a non-existing section. Should 210 never happen. */ 211 __libdw_seterrno (DWARF_E_INVALID_ELF); 212 free (result); 213 return NULL; 214 } 215 216 result = check_section (result, ehdr, scn, true); 217 if (result == NULL) 218 break; 219 } 220 221 return valid_p (result); 222} 223 224 225Dwarf * 226dwarf_begin_elf (elf, cmd, scngrp) 227 Elf *elf; 228 Dwarf_Cmd cmd; 229 Elf_Scn *scngrp; 230{ 231 GElf_Ehdr *ehdr; 232 GElf_Ehdr ehdr_mem; 233 234 /* Get the ELF header of the file. We need various pieces of 235 information from it. */ 236 ehdr = gelf_getehdr (elf, &ehdr_mem); 237 if (ehdr == NULL) 238 { 239 if (elf_kind (elf) != ELF_K_ELF) 240 __libdw_seterrno (DWARF_E_NOELF); 241 else 242 __libdw_seterrno (DWARF_E_GETEHDR_ERROR); 243 244 return NULL; 245 } 246 247 248 /* Default memory allocation size. */ 249 size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *); 250 251 /* Allocate the data structure. */ 252 Dwarf *result = (Dwarf *) calloc (1, sizeof (Dwarf) + mem_default_size); 253 if (result == NULL) 254 { 255 __libdw_seterrno (DWARF_E_NOMEM); 256 return NULL; 257 } 258 259 /* Fill in some values. */ 260 if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB) 261 || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB)) 262 result->other_byte_order = true; 263 264 result->elf = elf; 265 266 /* Initialize the memory handling. */ 267 result->mem_default_size = mem_default_size; 268 result->oom_handler = __libdw_oom; 269 result->mem_tail = (struct libdw_memblock *) (result + 1); 270 result->mem_tail->size = (result->mem_default_size 271 - offsetof (struct libdw_memblock, mem)); 272 result->mem_tail->remaining = result->mem_tail->size; 273 result->mem_tail->prev = NULL; 274 275 276 if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR) 277 { 278 /* If the caller provides a section group we get the DWARF 279 sections only from this setion group. Otherwise we search 280 for the first section with the required name. Further 281 sections with the name are ignored. The DWARF specification 282 does not really say this is allowed. */ 283 if (scngrp == NULL) 284 return global_read (result, elf, ehdr); 285 else 286 return scngrp_read (result, elf, ehdr, scngrp); 287 } 288 else if (cmd == DWARF_C_WRITE) 289 { 290 __libdw_seterrno (DWARF_E_UNIMPL); 291 free (result); 292 return NULL; 293 } 294 295 __libdw_seterrno (DWARF_E_INVALID_CMD); 296 free (result); 297 return NULL; 298} 299INTDEF(dwarf_begin_elf) 300