dwarf_begin_elf.c revision b08d5a8fb42f4586d756068065186b5af7e48da
1b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper/* Create descriptor from ELF descriptor for processing file.
2b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
3b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
5b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   This program is Open Source software; you can redistribute it and/or
6b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   modify it under the terms of the Open Software License version 1.0 as
7b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   published by the Open Source Initiative.
8b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
9b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   You should have received a copy of the Open Software License along
10b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   with this program; if not, you may obtain a copy of the Open Software
11b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   License version 1.0 from http://www.opensource.org/licenses/osl.php or
12b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper   3001 King Ranch Road, Ukiah, CA 95482.   */
14b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
15b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#ifdef HAVE_CONFIG_H
16b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper# include <config.h>
17b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#endif
18b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
19b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <stdbool.h>
20b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <stddef.h>
21b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <stdlib.h>
22b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <string.h>
23b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <unistd.h>
24b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include <sys/stat.h>
25b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
26b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#include "libdwP.h"
27b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
28b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
29b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper/* Section names.  */
30b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepperstatic const char dwarf_scnnames[IDX_last][17] =
31b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
32b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_info] = ".debug_info",
33b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_abbrev] = ".debug_abbrev",
34b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_aranges] = ".debug_aranges",
35b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_line] = ".debug_line",
36b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_frame] = ".debug_frame",
37b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_eh_frame] = ".eh_frame",
38b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_loc] = ".debug_loc",
39b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_pubnames] = ".debug_pubnames",
40b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_str] = ".debug_str",
41b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_funcnames] = ".debug_funcnames",
42b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_typenames] = ".debug_typenames",
43b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_varnames] = ".debug_varnames",
44b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_weaknames] = ".debug_weaknames",
45b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_macinfo] = ".debug_macinfo",
46b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  [IDX_debug_ranges] = ".debug_ranges"
47b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper};
48b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper#define ndwarf_scnnames (sizeof (dwarf_scnnames) / sizeof (dwarf_scnnames[0]))
49b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
50b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
51b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepperstatic void
52b08d5a8fb42f4586d756068065186b5af7e48daUlrich Dreppercheck_section (Dwarf *result, GElf_Ehdr *ehdr, Elf_Scn *scn, bool inscngrp)
53b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
54b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  GElf_Shdr shdr_mem;
55b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  GElf_Shdr *shdr;
56b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
57b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* Get the section header data.  */
58b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  shdr = gelf_getshdr (scn, &shdr_mem);
59b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (shdr == NULL)
60b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    /* This should never happen.  If it does something is
61b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper       wrong in the libelf library.  */
62b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    abort ();
63b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
64b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
65b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* Make sure the section is part of a section group only iff we
66b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     really need it.  If we are looking for the global (= non-section
67b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     group debug info) we have to ignore all the info in section
68b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     groups.  If we are looking into a section group we cannot look at
69b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     a section which isn't part of the section group.  */
70b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (! inscngrp && (shdr->sh_flags & SHF_GROUP) != 0)
71b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    /* Ignore the section.  */
72b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    return;
73b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
74b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
75b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* We recognize the DWARF section by their names.  This is not very
76b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     safe and stable but the best we can do.  */
77b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  const char *scnname = elf_strptr (result->elf, ehdr->e_shstrndx,
78b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper				    shdr->sh_name);
79b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (scnname == NULL)
80b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
81b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      /* The section name must be valid.  Otherwise is the ELF file
82b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	 invalid.  */
83b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      __libdw_seterrno (DWARF_E_INVALID_ELF);
84b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      free (result);
85b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      return;
86b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
87b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
88b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
89b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* Recognize the various sections.  Most names start with .debug_.  */
90b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  size_t cnt;
91b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  for (cnt = 0; cnt < ndwarf_scnnames; ++cnt)
92b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    if (strcmp (scnname, dwarf_scnnames[cnt]) == 0)
93b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      {
94b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	/* Found it.  Remember where the data is.  */
95b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	if (unlikely (result->sectiondata[cnt] != NULL))
96b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* A section appears twice.  That's bad.  We ignore the section.  */
97b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  break;
98b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
99b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	/* Get the section data.  */
100b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	Elf_Data *data = elf_getdata (scn, NULL);
101b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	if (data != NULL && data->d_size != 0)
102b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* Yep, there is actually data available.  */
103b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  result->sectiondata[cnt] = data;
104b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
105b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	break;
106b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      }
107b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper}
108b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
109b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
110b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper/* Check whether all the necessary DWARF information is available.  */
111b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepperstatic Dwarf *
112b08d5a8fb42f4586d756068065186b5af7e48daUlrich Dreppervalid_p (Dwarf *result)
113b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
114b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* We looked at all the sections.  Now determine whether all the
115b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     sections with debugging information we need are there.
116b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
117b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     XXX Which sections are absolutely necessary?  Add tests if
118b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     necessary.  For now we require only .debug_info.  Hopefully this
119b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     is correct.  */
120b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (unlikely (result->sectiondata[IDX_debug_info] == NULL))
121b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
122b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      __libdw_seterrno (DWARF_E_NO_DWARF);
123b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      result = NULL;
124b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
125b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
126b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  return result;
127b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper}
128b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
129b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
130b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepperstatic Dwarf *
131b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepperglobal_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr)
132b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
133b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  Elf_Scn *scn = NULL;
134b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
135b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  while ((scn = elf_nextscn (elf, scn)) != NULL)
136b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    check_section (result, ehdr, scn, false);
137b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
138b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  return valid_p (result);
139b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper}
140b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
141b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
142b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepperstatic Dwarf *
143b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepperscngrp_read (Dwarf *result, Elf *elf, GElf_Ehdr *ehdr, Elf_Scn *scngrp)
144b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
145b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* SCNGRP is the section descriptor for a section group which might
146b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     contain debug sections.  */
147b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  Elf_Data *data = elf_getdata (scngrp, NULL);
148b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (data == NULL)
149b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
150b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      /* We cannot read the section content.  Fail!  */
151b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      free (result);
152b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      return NULL;
153b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
154b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
155b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* The content of the section is a number of 32-bit words which
156b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     represent section indices.  The first word is a flag word.  */
157b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  Elf32_Word *scnidx = (Elf32_Word *) data->d_buf;
158b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  size_t cnt;
159b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  for (cnt = 1; cnt * sizeof (Elf32_Word) <= data->d_size; ++cnt)
160b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
161b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      Elf_Scn *scn = elf_getscn (elf, scnidx[cnt]);
162b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      if (scn == NULL)
163b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	{
164b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  /* A section group refers to a non-existing section.  Should
165b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	     never happen.  */
166b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  __libdw_seterrno (DWARF_E_INVALID_ELF);
167b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  free (result);
168b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	  return NULL;
169b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	}
170b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
171b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      check_section (result, ehdr, scn, true);
172b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
173b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
174b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  return valid_p (result);
175b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper}
176b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
177b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
178b08d5a8fb42f4586d756068065186b5af7e48daUlrich DrepperDwarf *
179b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepperdwarf_begin_elf (elf, cmd, scngrp)
180b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     Elf *elf;
181b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     Dwarf_Cmd cmd;
182b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     Elf_Scn *scngrp;
183b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper{
184b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  GElf_Ehdr *ehdr;
185b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  GElf_Ehdr ehdr_mem;
186b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
187b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* Get the ELF header of the file.  We need various pieces of
188b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper     information from it.  */
189b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  ehdr = gelf_getehdr (elf, &ehdr_mem);
190b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (ehdr == NULL)
191b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
192b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      if (elf_kind (elf) != ELF_K_ELF)
193b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	__libdw_seterrno (DWARF_E_NOELF);
194b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      else
195b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	__libdw_seterrno (DWARF_E_GETEHDR_ERROR);
196b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
197b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      return NULL;
198b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
199b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
200b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
201b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* Default memory allocation size.  */
202b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
203b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
204b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* Allocate the data structure.  */
205b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  Dwarf *result = (Dwarf *) calloc (1, sizeof (Dwarf) + mem_default_size);
206b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (result == NULL)
207b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
208b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      __libdw_seterrno (DWARF_E_NOMEM);
209b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      return NULL;
210b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
211b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
212b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* Fill in some values.  */
213b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if ((BYTE_ORDER == LITTLE_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2MSB)
214b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      || (BYTE_ORDER == BIG_ENDIAN && ehdr->e_ident[EI_DATA] == ELFDATA2LSB))
215b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    result->other_byte_order = true;
216b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
217b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  result->elf = elf;
218b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
219b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  /* Initialize the memory handling.  */
220b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  result->mem_default_size = mem_default_size;
221b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  result->oom_handler = __libdw_oom;
222b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  result->mem_tail = (struct libdw_memblock *) (result + 1);
223b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  result->mem_tail->size = (result->mem_default_size
224b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper			    - offsetof (struct libdw_memblock, mem));
225b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  result->mem_tail->remaining = result->mem_tail->size;
226b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  result->mem_tail->prev = NULL;
227b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
228b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
229b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  if (cmd == DWARF_C_READ || cmd == DWARF_C_RDWR)
230b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
231b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      /* If the caller provides a section group we get the DWARF
232b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	 sections only from this setion group.  Otherwise we search
233b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	 for the first section with the required name.  Further
234b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	 sections with the name are ignored.  The DWARF specification
235b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	 does not really say this is allowed.  */
236b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      if (scngrp == NULL)
237b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	return global_read (result, elf, ehdr);
238b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      else
239b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper	return scngrp_read (result, elf, ehdr, scngrp);
240b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
241b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  else if (cmd == DWARF_C_WRITE)
242b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    {
243b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      __libdw_seterrno (DWARF_E_UNIMPL);
244b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      free (result);
245b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper      return NULL;
246b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper    }
247b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper
248b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  __libdw_seterrno (DWARF_E_INVALID_CMD);
249b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  free (result);
250b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper  return NULL;
251b08d5a8fb42f4586d756068065186b5af7e48daUlrich Drepper}
252b08d5a8fb42f4586d756068065186b5af7e48daUlrich DrepperINTDEF(dwarf_begin_elf)
253