2 rawdata.c - implementation of the elf_rawdata(3) function.
3 Copyright (C) 1995 - 2000 Michael Riepe
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 static const char rcsid[] = "@(#) $Id: rawdata.c,v 1.10 2008/05/23 08:15:35 michael Exp $";
27 elf_rawdata(Elf_Scn *scn, Elf_Data *data) {
34 elf_assert(scn->s_magic == SCN_MAGIC);
37 elf_assert(elf->e_magic == ELF_MAGIC);
38 if (!elf->e_readable) {
41 else if (scn->s_index == SHN_UNDEF || scn->s_type == SHT_NULL) {
42 seterr(ERROR_NULLSCN);
47 else if ((sd = scn->s_rawdata)) {
48 elf_assert(sd->sd_magic == DATA_MAGIC);
49 elf_assert(sd->sd_scn == scn);
52 else if (scn->s_offset < 0 || scn->s_offset > elf->e_size) {
53 seterr(ERROR_OUTSIDE);
55 else if (scn->s_type != SHT_NOBITS
56 && scn->s_offset + scn->s_size > elf->e_size) {
57 seterr(ERROR_TRUNC_SCN);
59 else if (!(sd = (Scn_Data*)malloc(sizeof(*sd)))) {
60 seterr(ERROR_MEM_SCNDATA);
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);
74 else if (elf->e_rawdata) {
75 memcpy(sd->sd_memdata, elf->e_rawdata + scn->s_offset, scn->s_size);
77 else if (!_elf_read(elf, sd->sd_memdata, scn->s_offset, scn->s_size)) {
82 sd->sd_data.d_buf = sd->sd_memdata;