blob: 70a1b07dcbce6a84ba7fc76c07ad0d6f42fd22bb [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Create descriptor from file descriptor for processing file.
2 Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 This program is Open Source software; you can redistribute it and/or
6 modify it under the terms of the Open Software License version 1.0 as
7 published by the Open Source Initiative.
8
9 You should have received a copy of the Open Software License along
10 with this program; if not, you may obtain a copy of the Open Software
11 License version 1.0 from http://www.opensource.org/licenses/osl.php or
12 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13 3001 King Ranch Road, Ukiah, CA 95482. */
14
15#ifdef HAVE_CONFIG_H
16# include <config.h>
17#endif
18
19#include <errno.h>
20#include <stddef.h>
21#include <sys/stat.h>
22
23#include <libdwP.h>
24
25
26Dwarf *
27dwarf_begin (fd, cmd)
28 int fd;
29 Dwarf_Cmd cmd;
30{
31 Elf *elf;
32 Elf_Cmd elfcmd;
33 Dwarf *result = NULL;
34
35 switch (cmd)
36 {
37 case DWARF_C_READ:
38 elfcmd = ELF_C_READ_MMAP;
39 break;
40 case DWARF_C_WRITE:
41 elfcmd = ELF_C_WRITE;
42 break;
43 case DWARF_C_RDWR:
44 elfcmd = ELF_C_RDWR;
45 break;
46 default:
47 /* No valid mode. */
48 __libdw_seterrno (DWARF_E_INVALID_CMD);
49 return NULL;
50 }
51
52 /* We have to call `elf_version' here since the user might have not
53 done it or initialized libelf with a different version. This
54 would break libdwarf since we are using the ELF data structures
55 in a certain way. */
56 elf_version (EV_CURRENT);
57
58 /* Get an ELF descriptor. */
59 elf = elf_begin (fd, elfcmd, NULL);
60 if (elf == NULL)
61 {
62 /* Test why the `elf_begin" call failed. */
63 struct stat64 st;
64
65 if (fstat64 (fd, &st) == 0 && ! S_ISREG (st.st_mode))
66 __libdw_seterrno (DWARF_E_NO_REGFILE);
67 else if (errno == EBADF)
68 __libdw_seterrno (DWARF_E_INVALID_FILE);
69 else
70 __libdw_seterrno (DWARF_E_IO_ERROR);
71 }
72 else
73 {
74 /* Do the real work now that we have an ELF descriptor. */
75 result = INTUSE(dwarf_begin_elf) (elf, cmd, NULL);
76
77 /* If this failed, free the resources. */
78 if (result == NULL)
79 elf_end (elf);
80 else
81 result->free_elf = true;
82 }
83
84 return result;
85}