1 /* Retrieve uninterpreted chunk of the file contents. 2 Copyright (C) 2002 Red Hat, Inc. 3 Contributed by Ulrich Drepper <drepper (at) redhat.com>, 2002. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, version 2. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program; if not, write to the Free Software Foundation, 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17 18 #ifdef HAVE_CONFIG_H 19 # include <config.h> 20 #endif 21 22 #include <libelf.h> 23 #include <stddef.h> 24 #include <stdlib.h> 25 #include <unistd.h> 26 27 #include "libelfP.h" 28 29 30 char * 31 gelf_rawchunk (elf, offset, size) 32 Elf *elf; 33 GElf_Off offset; 34 GElf_Word size; 35 { 36 if (elf == NULL) 37 { 38 /* No valid descriptor. */ 39 __libelf_seterrno (ELF_E_INVALID_HANDLE); 40 return NULL; 41 } 42 43 if (offset >= elf->maximum_size 44 || offset + size >= elf->maximum_size 45 || offset + size < offset) 46 { 47 /* Invalid request. */ 48 __libelf_seterrno (ELF_E_INVALID_OP); 49 return NULL; 50 } 51 52 /* If the file is mmap'ed return an appropriate pointer. */ 53 if (elf->map_address != NULL) 54 return (char *) elf->map_address + elf->start_offset + offset; 55 56 /* We allocate the memory and read the data from the file. */ 57 char *result = (char *) malloc (size); 58 if (result == NULL) 59 __libelf_seterrno (ELF_E_NOMEM); 60 else 61 /* Read the file content. */ 62 if ((size_t) pread (elf->fildes, result, size, elf->start_offset + offset) 63 != size) 64 { 65 /* Something went wrong. */ 66 __libelf_seterrno (ELF_E_READ_ERROR); 67 free (result); 68 } 69 70 return result; 71 } 72