|
1 /* |
|
2 rawdata.c - implementation of the elf_rawdata(3) function. |
|
3 Copyright (C) 1995 - 2000 Michael Riepe |
|
4 |
|
5 This library is free software; you can redistribute it and/or |
|
6 modify it under the terms of the GNU Library General Public |
|
7 License as published by the Free Software Foundation; either |
|
8 version 2 of the License, or (at your option) any later version. |
|
9 |
|
10 This library is distributed in the hope that it will be useful, |
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 Library General Public License for more details. |
|
14 |
|
15 You should have received a copy of the GNU Library General Public |
|
16 License along with this library; if not, write to the Free Software |
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 */ |
|
19 |
|
20 #include <private.h> |
|
21 |
|
22 #ifndef lint |
|
23 static const char rcsid[] = "@(#) $Id: rawdata.c,v 1.9 2005/05/21 15:39:26 michael Exp $"; |
|
24 #endif /* lint */ |
|
25 |
|
26 Elf_Data* |
|
27 elf_rawdata(Elf_Scn *scn, Elf_Data *data) { |
|
28 Scn_Data *sd; |
|
29 Elf *elf; |
|
30 |
|
31 if (!scn) { |
|
32 return NULL; |
|
33 } |
|
34 elf_assert(scn->s_magic == SCN_MAGIC); |
|
35 elf = scn->s_elf; |
|
36 elf_assert(elf); |
|
37 elf_assert(elf->e_magic == ELF_MAGIC); |
|
38 if (!elf->e_readable) { |
|
39 return NULL; |
|
40 } |
|
41 else if (scn->s_index == SHN_UNDEF || scn->s_type == SHT_NULL) { |
|
42 seterr(ERROR_NULLSCN); |
|
43 } |
|
44 else if (data) { |
|
45 return NULL; |
|
46 } |
|
47 else if ((sd = scn->s_rawdata)) { |
|
48 elf_assert(sd->sd_magic == DATA_MAGIC); |
|
49 elf_assert(sd->sd_scn == scn); |
|
50 return &sd->sd_data; |
|
51 } |
|
52 else if (scn->s_offset < 0 || scn->s_offset > elf->e_size) { |
|
53 seterr(ERROR_OUTSIDE); |
|
54 } |
|
55 else if (scn->s_type != SHT_NOBITS |
|
56 && scn->s_offset + scn->s_size > elf->e_size) { |
|
57 seterr(ERROR_TRUNC_SCN); |
|
58 } |
|
59 else if (!(sd = (Scn_Data*)malloc(sizeof(*sd)))) { |
|
60 seterr(ERROR_MEM_SCNDATA); |
|
61 } |
|
62 else { |
|
63 *sd = _elf_data_init; |
|
64 sd->sd_scn = scn; |
|
65 sd->sd_freeme = 1; |
|
66 sd->sd_data.d_size = scn->s_size; |
|
67 sd->sd_data.d_version = _elf_version; |
|
68 if (scn->s_type != SHT_NOBITS && scn->s_size) { |
|
69 if (!(sd->sd_memdata = (char*)malloc(scn->s_size))) { |
|
70 seterr(ERROR_IO_2BIG); |
|
71 free(sd); |
|
72 return NULL; |
|
73 } |
|
74 else if (elf->e_rawdata) { |
|
75 memcpy(sd->sd_memdata, elf->e_rawdata + scn->s_offset, scn->s_size); |
|
76 } |
|
77 else if (!_elf_read(elf, sd->sd_memdata, scn->s_offset, scn->s_size)) { |
|
78 free(sd->sd_memdata); |
|
79 free(sd); |
|
80 return NULL; |
|
81 } |
|
82 sd->sd_data.d_buf = sd->sd_memdata; |
|
83 sd->sd_free_data = 1; |
|
84 } |
|
85 scn->s_rawdata = sd; |
|
86 return &sd->sd_data; |
|
87 } |
|
88 return NULL; |
|
89 } |