libdwfl: Support automatic decompression of files in XZ format.
[platform/upstream/elfutils.git] / libdwfl / open.c
1 /* Decompression support for libdwfl: zlib (gzip) and/or bzlib (bzip2).
2    Copyright (C) 2009 Red Hat, Inc.
3    This file is part of Red Hat elfutils.
4
5    Red Hat elfutils is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by the
7    Free Software Foundation; version 2 of the License.
8
9    Red Hat elfutils is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with Red Hat elfutils; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17
18    In addition, as a special exception, Red Hat, Inc. gives You the
19    additional right to link the code of Red Hat elfutils with code licensed
20    under any Open Source Initiative certified open source license
21    (http://www.opensource.org/licenses/index.php) which requires the
22    distribution of source code with any binary distribution and to
23    distribute linked combinations of the two.  Non-GPL Code permitted under
24    this exception must only link to the code of Red Hat elfutils through
25    those well defined interfaces identified in the file named EXCEPTION
26    found in the source code files (the "Approved Interfaces").  The files
27    of Non-GPL Code may instantiate templates or use macros or inline
28    functions from the Approved Interfaces without causing the resulting
29    work to be covered by the GNU General Public License.  Only Red Hat,
30    Inc. may make changes or additions to the list of Approved Interfaces.
31    Red Hat's grant of this exception is conditioned upon your not adding
32    any new exceptions.  If you wish to add a new Approved Interface or
33    exception, please contact Red Hat.  You must obey the GNU General Public
34    License in all respects for all of the Red Hat elfutils code and other
35    code used in conjunction with Red Hat elfutils except the Non-GPL Code
36    covered by this exception.  If you modify this file, you may extend this
37    exception to your version of the file, but you are not obligated to do
38    so.  If you do not wish to provide this exception without modification,
39    you must delete this exception statement from your version and license
40    this file solely under the GPL without exception.
41
42    Red Hat elfutils is an included package of the Open Invention Network.
43    An included package of the Open Invention Network is a package for which
44    Open Invention Network licensees cross-license their patents.  No patent
45    license is granted, either expressly or impliedly, by designation as an
46    included package.  Should you wish to participate in the Open Invention
47    Network licensing program, please visit www.openinventionnetwork.com
48    <http://www.openinventionnetwork.com>.  */
49
50 #include "../libelf/libelfP.h"
51 #undef  _
52 #include "libdwflP.h"
53
54 #include <unistd.h>
55
56 #if !USE_ZLIB
57 # define __libdw_gunzip(...)    false
58 #endif
59
60 #if !USE_BZLIB
61 # define __libdw_bunzip2(...)   false
62 #endif
63
64 #if !USE_LZMA
65 # define __libdw_unlzma(...)    false
66 #endif
67
68 /* Always consumes *ELF, never consumes FD.
69    Replaces *ELF on success.  */
70 static Dwfl_Error
71 decompress (int fd __attribute__ ((unused)), Elf **elf)
72 {
73   Dwfl_Error error = DWFL_E_BADELF;
74   void *buffer = NULL;
75   size_t size = 0;
76
77 #if USE_ZLIB || USE_BZLIB || USE_LZMA
78   const off64_t offset = (*elf)->start_offset;
79   void *const mapped = ((*elf)->map_address == NULL ? NULL
80                         : (*elf)->map_address + (*elf)->start_offset);
81   const size_t mapped_size = (*elf)->maximum_size;
82   if (mapped_size == 0)
83     return error;
84
85   error = __libdw_gunzip (fd, offset, mapped, mapped_size, &buffer, &size);
86   if (error == DWFL_E_BADELF)
87     error = __libdw_bunzip2 (fd, offset, mapped, mapped_size, &buffer, &size);
88   if (error == DWFL_E_BADELF)
89     error = __libdw_unlzma (fd, offset, mapped, mapped_size, &buffer, &size);
90 #endif
91
92   elf_end (*elf);
93   *elf = NULL;
94
95   if (error == DWFL_E_NOERROR)
96     {
97       if (unlikely (size == 0))
98         {
99           error = DWFL_E_BADELF;
100           free (buffer);
101         }
102       else
103         {
104           *elf = elf_memory (buffer, size);
105           if (*elf == NULL)
106             {
107               error = DWFL_E_LIBELF;
108               free (buffer);
109             }
110           else
111             (*elf)->flags |= ELF_F_MALLOCED;
112         }
113     }
114
115   return error;
116 }
117
118 Dwfl_Error internal_function
119 __libdw_open_file (int *fdp, Elf **elfp, bool close_on_fail, bool archive_ok)
120 {
121   bool close_fd = false;
122   Dwfl_Error error = DWFL_E_NOERROR;
123
124   Elf *elf = elf_begin (*fdp, ELF_C_READ_MMAP_PRIVATE, NULL);
125   Elf_Kind kind = elf_kind (elf);
126   if (unlikely (kind == ELF_K_NONE))
127     {
128       if (unlikely (elf == NULL))
129         error = DWFL_E_LIBELF;
130       else
131         {
132           error = decompress (*fdp, &elf);
133           if (error == DWFL_E_NOERROR)
134             {
135               close_fd = true;
136               kind = elf_kind (elf);
137             }
138         }
139     }
140
141   if (error == DWFL_E_NOERROR
142       && kind != ELF_K_ELF
143       && !(archive_ok && kind == ELF_K_AR))
144     {
145       elf_end (elf);
146       elf = NULL;
147       error = DWFL_E_BADELF;
148     }
149
150   if (error == DWFL_E_NOERROR ? close_fd : close_on_fail)
151     {
152       close (*fdp);
153       *fdp = -1;
154     }
155
156   *elfp = elf;
157   return error;
158 }