Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame^] | 1 | /* Create descriptor from ELF descriptor for processing file. |
| 2 | Copyright (C) 2002-2011, 2014, 2015, 2018 Red Hat, Inc. |
| 3 | This file is part of elfutils. |
| 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 5 | |
| 6 | This file is free software; you can redistribute it and/or modify |
| 7 | it under the terms of either |
| 8 | |
| 9 | * the GNU Lesser General Public License as published by the Free |
| 10 | Software Foundation; either version 3 of the License, or (at |
| 11 | your option) any later version |
| 12 | |
| 13 | or |
| 14 | |
| 15 | * the GNU General Public License as published by the Free |
| 16 | Software Foundation; either version 2 of the License, or (at |
| 17 | your option) any later version |
| 18 | |
| 19 | or both in parallel, as here. |
| 20 | |
| 21 | elfutils is distributed in the hope that it will be useful, but |
| 22 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | General Public License for more details. |
| 25 | |
| 26 | You should have received copies of the GNU General Public License and |
| 27 | the GNU Lesser General Public License along with this program. If |
| 28 | not, see <http://www.gnu.org/licenses/>. */ |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | # include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <assert.h> |
| 35 | #include <stdbool.h> |
| 36 | #include <stddef.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <stdio.h> |
| 39 | #include <string.h> |
| 40 | #include <unistd.h> |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <fcntl.h> |
| 44 | #include <endian.h> |
| 45 | |
| 46 | #include "libdwP.h" |
| 47 | |
| 48 | |
| 49 | /* Section names. */ |
| 50 | static const char dwarf_scnnames[IDX_last][18] = |
| 51 | { |
| 52 | [IDX_debug_info] = ".debug_info", |
| 53 | [IDX_debug_types] = ".debug_types", |
| 54 | [IDX_debug_abbrev] = ".debug_abbrev", |
| 55 | [IDX_debug_aranges] = ".debug_aranges", |
| 56 | [IDX_debug_line] = ".debug_line", |
| 57 | [IDX_debug_frame] = ".debug_frame", |
| 58 | [IDX_debug_loc] = ".debug_loc", |
| 59 | [IDX_debug_pubnames] = ".debug_pubnames", |
| 60 | [IDX_debug_str] = ".debug_str", |
| 61 | [IDX_debug_macinfo] = ".debug_macinfo", |
| 62 | [IDX_debug_macro] = ".debug_macro", |
| 63 | [IDX_debug_ranges] = ".debug_ranges", |
| 64 | [IDX_gnu_debugaltlink] = ".gnu_debugaltlink" |
| 65 | }; |
| 66 | #define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0])) |
| 67 | |
| 68 | static Dwarf * |
| 69 | check_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp) |
| 70 | { |
| 71 | GElf_Shdr shdr_mem; |
| 72 | GElf_Shdr *shdr; |
| 73 | |
| 74 | /* Get the section header data. */ |
| 75 | shdr = gelf_getshdr (scn, &shdr_mem); |
| 76 | if (shdr == NULL) |
| 77 | /* We may read /proc/PID/mem with only program headers mapped and section |
| 78 | headers out of the mapped pages. */ |
| 79 | goto err; |
| 80 | |
| 81 | /* Ignore any SHT_NOBITS sections. Debugging sections should not |
| 82 | have been stripped, but in case of a corrupt file we won't try |
| 83 | to look at the missing data. */ |
| 84 | if (unlikely (shdr->sh_type == SHT_NOBITS)) |
| 85 | return result; |
| 86 | |
| 87 | /* Make sure the section is part of a section group only iff we |
| 88 | really need it. If we are looking for the global (= non-section |
| 89 | group debug info) we have to ignore all the info in section |
| 90 | groups. If we are looking into a section group we cannot look at |
| 91 | a section which isn't part of the section group. */ |
| 92 | if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0) |
| 93 | /* Ignore the section. */ |
| 94 | return result; |
| 95 | |
| 96 | |
| 97 | /* We recognize the DWARF section by their names. This is not very |
| 98 | safe and stable but the best we can do. */ |
| 99 | const char *scnname = elf_strptr (result->elf, ehdr->e_shstrndx, |
| 100 | shdr->sh_name); |
| 101 | if (scnname == NULL) |
| 102 | { |
| 103 | /* The section name must be valid. Otherwise is the ELF file |
| 104 | invalid. */ |
| 105 | err: |
| 106 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 107 | __libdw_seterrno (DWARF_E_INVALID_ELF); |
| 108 | free (result); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | /* Recognize the various sections. Most names start with .debug_. */ |
| 113 | size_t cnt; |
| 114 | bool gnu_compressed = false; |
| 115 | for (cnt = 0; cnt < ndwarf_scnnames; ++cnt) |
| 116 | if (strcmp (scnname, dwarf_scnnames[cnt]) == 0) |
| 117 | break; |
| 118 | else if (scnname[0] == '.' && scnname[1] == 'z' |
| 119 | && strcmp (&scnname[2], &dwarf_scnnames[cnt][1]) == 0) |
| 120 | { |
| 121 | gnu_compressed = true; |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | if (cnt >= ndwarf_scnnames) |
| 126 | /* Not a debug section; ignore it. */ |
| 127 | return result; |
| 128 | |
| 129 | if (unlikely (result->sectiondata[cnt] != NULL)) |
| 130 | /* A section appears twice. That's bad. We ignore the section. */ |
| 131 | return result; |
| 132 | |
| 133 | /* We cannot know whether or not a GNU compressed section has already |
| 134 | been uncompressed or not, so ignore any errors. */ |
| 135 | if (gnu_compressed) |
| 136 | elf_compress_gnu (scn, 0, 0); |
| 137 | |
| 138 | if ((shdr->sh_flags & SHF_COMPRESSED) != 0) |
| 139 | { |
| 140 | if (elf_compress (scn, 0, 0) < 0) |
| 141 | { |
| 142 | /* If we failed to decompress the section and it's the |
| 143 | debug_info section, then fail with specific error rather |
| 144 | than the generic NO_DWARF. Without debug_info we can't do |
| 145 | anything (see also valid_p()). */ |
| 146 | if (cnt == IDX_debug_info) |
| 147 | { |
| 148 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 149 | __libdw_seterrno (DWARF_E_COMPRESSED_ERROR); |
| 150 | free (result); |
| 151 | return NULL; |
| 152 | } |
| 153 | return result; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* Get the section data. */ |
| 158 | Elf_Data *data = elf_getdata (scn, NULL); |
| 159 | if (data == NULL) |
| 160 | goto err; |
| 161 | |
| 162 | if (data->d_buf == NULL || data->d_size == 0) |
| 163 | /* No data actually available, ignore it. */ |
| 164 | return result; |
| 165 | |
| 166 | /* We can now read the section data into results. */ |
| 167 | result->sectiondata[cnt] = data; |
| 168 | |
| 169 | return result; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /* Check whether all the necessary DWARF information is available. */ |
| 174 | static Dwarf * |
| 175 | valid_p (Dwarf *result) |
| 176 | { |
| 177 | /* We looked at all the sections. Now determine whether all the |
| 178 | sections with debugging information we need are there. |
| 179 | |
| 180 | XXX Which sections are absolutely necessary? Add tests if |
| 181 | necessary. For now we require only .debug_info. Hopefully this |
| 182 | is correct. */ |
| 183 | if (likely (result != NULL) |
| 184 | && unlikely (result->sectiondata[IDX_debug_info] == NULL)) |
| 185 | { |
| 186 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 187 | __libdw_seterrno (DWARF_E_NO_DWARF); |
| 188 | free (result); |
| 189 | result = NULL; |
| 190 | } |
| 191 | |
| 192 | if (result != NULL && result->sectiondata[IDX_debug_loc] != NULL) |
| 193 | { |
| 194 | result->fake_loc_cu = (Dwarf_CU *) calloc (1, sizeof (Dwarf_CU)); |
| 195 | if (unlikely (result->fake_loc_cu == NULL)) |
| 196 | { |
| 197 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 198 | __libdw_seterrno (DWARF_E_NOMEM); |
| 199 | free (result); |
| 200 | result = NULL; |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | result->fake_loc_cu->sec_idx = IDX_debug_loc; |
| 205 | result->fake_loc_cu->dbg = result; |
| 206 | result->fake_loc_cu->startp |
| 207 | = result->sectiondata[IDX_debug_loc]->d_buf; |
| 208 | result->fake_loc_cu->endp |
| 209 | = (result->sectiondata[IDX_debug_loc]->d_buf |
| 210 | + result->sectiondata[IDX_debug_loc]->d_size); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return result; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | static Dwarf * |
| 219 | global_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr) |
| 220 | { |
| 221 | Elf_Scn *scn = NULL; |
| 222 | |
| 223 | while (result != NULL && (scn = elf_nextscn (elf, scn)) != NULL) |
| 224 | result = check_section (result, ehdr, scn, false); |
| 225 | |
| 226 | return valid_p (result); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | static Dwarf * |
| 231 | scngrp_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr, Elf_Scn *scngrp) |
| 232 | { |
| 233 | GElf_Shdr shdr_mem; |
| 234 | GElf_Shdr *shdr = gelf_getshdr (scngrp, &shdr_mem); |
| 235 | if (shdr == NULL) |
| 236 | { |
| 237 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 238 | __libdw_seterrno (DWARF_E_INVALID_ELF); |
| 239 | free (result); |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | if ((shdr->sh_flags & SHF_COMPRESSED) != 0 |
| 244 | && elf_compress (scngrp, 0, 0) < 0) |
| 245 | { |
| 246 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 247 | __libdw_seterrno (DWARF_E_COMPRESSED_ERROR); |
| 248 | free (result); |
| 249 | return NULL; |
| 250 | } |
| 251 | |
| 252 | /* SCNGRP is the section descriptor for a section group which might |
| 253 | contain debug sections. */ |
| 254 | Elf_Data *data = elf_getdata (scngrp, NULL); |
| 255 | if (data == NULL) |
| 256 | { |
| 257 | /* We cannot read the section content. Fail! */ |
| 258 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 259 | free (result); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | /* The content of the section is a number of 32-bit words which |
| 264 | represent section indices. The first word is a flag word. */ |
| 265 | Elf32_Word *scnidx = (Elf32_Word *) data->d_buf; |
| 266 | size_t cnt; |
| 267 | for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt) |
| 268 | { |
| 269 | Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]); |
| 270 | if (scn == NULL) |
| 271 | { |
| 272 | /* A section group refers to a non-existing section. Should |
| 273 | never happen. */ |
| 274 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 275 | __libdw_seterrno (DWARF_E_INVALID_ELF); |
| 276 | free (result); |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | result = check_section (result, ehdr, scn, true); |
| 281 | if (result == NULL) |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | return valid_p (result); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | Dwarf * |
| 290 | dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp) |
| 291 | { |
| 292 | GElf_Ehdr *ehdr; |
| 293 | GElf_Ehdr ehdr_mem; |
| 294 | |
| 295 | /* Get the ELF header of the file. We need various pieces of |
| 296 | information from it. */ |
| 297 | ehdr = gelf_getehdr (elf, &ehdr_mem); |
| 298 | if (ehdr == NULL) |
| 299 | { |
| 300 | if (elf_kind (elf) != ELF_K_ELF) |
| 301 | __libdw_seterrno (DWARF_E_NOELF); |
| 302 | else |
| 303 | __libdw_seterrno (DWARF_E_GETEHDR_ERROR); |
| 304 | |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | /* Default memory allocation size. */ |
| 310 | size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *); |
| 311 | assert (sizeof (struct Dwarf) < mem_default_size); |
| 312 | |
| 313 | /* Allocate the data structure. */ |
| 314 | Dwarf *result = (Dwarf *) calloc (1, sizeof (Dwarf) + mem_default_size); |
| 315 | if (unlikely (result == NULL) |
| 316 | || unlikely (Dwarf_Sig8_Hash_init (&result->sig8_hash, 11) < 0)) |
| 317 | { |
| 318 | free (result); |
| 319 | __libdw_seterrno (DWARF_E_NOMEM); |
| 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | /* Fill in some values. */ |
| 324 | if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB) |
| 325 | || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB)) |
| 326 | result->other_byte_order = true; |
| 327 | |
| 328 | result->elf = elf; |
| 329 | result->alt_fd = -1; |
| 330 | |
| 331 | /* Initialize the memory handling. */ |
| 332 | result->mem_default_size = mem_default_size; |
| 333 | result->oom_handler = __libdw_oom; |
| 334 | result->mem_tail = (struct libdw_memblock *) (result + 1); |
| 335 | result->mem_tail->size = (result->mem_default_size |
| 336 | - offsetof (struct libdw_memblock, mem)); |
| 337 | result->mem_tail->remaining = result->mem_tail->size; |
| 338 | result->mem_tail->prev = NULL; |
| 339 | |
| 340 | if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR) |
| 341 | { |
| 342 | /* If the caller provides a section group we get the DWARF |
| 343 | sections only from this setion group. Otherwise we search |
| 344 | for the first section with the required name. Further |
| 345 | sections with the name are ignored. The DWARF specification |
| 346 | does not really say this is allowed. */ |
| 347 | if (scngrp == NULL) |
| 348 | return global_read (result, elf, ehdr); |
| 349 | else |
| 350 | return scngrp_read (result, elf, ehdr, scngrp); |
| 351 | } |
| 352 | else if (cmd == DWARF_C_WRITE) |
| 353 | { |
| 354 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 355 | __libdw_seterrno (DWARF_E_UNIMPL); |
| 356 | free (result); |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | Dwarf_Sig8_Hash_free (&result->sig8_hash); |
| 361 | __libdw_seterrno (DWARF_E_INVALID_CMD); |
| 362 | free (result); |
| 363 | return NULL; |
| 364 | } |
| 365 | INTDEF(dwarf_begin_elf) |