1/* Return section compression header. 2 Copyright (C) 2015 Red Hat, Inc. 3 This file is part of elfutils. 4 5 This file is free software; you can redistribute it and/or modify 6 it under the terms of either 7 8 * the GNU Lesser General Public License as published by the Free 9 Software Foundation; either version 3 of the License, or (at 10 your option) any later version 11 12 or 13 14 * the GNU General Public License as published by the Free 15 Software Foundation; either version 2 of the License, or (at 16 your option) any later version 17 18 or both in parallel, as here. 19 20 elfutils is distributed in the hope that it will be useful, but 21 WITHOUT ANY WARRANTY; without even the implied warranty of 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 General Public License for more details. 24 25 You should have received copies of the GNU General Public License and 26 the GNU Lesser General Public License along with this program. If 27 not, see <http://www.gnu.org/licenses/>. */ 28 29#ifdef HAVE_CONFIG_H 30# include <config.h> 31#endif 32 33#include <libelf.h> 34#include "libelfP.h" 35#include "common.h" 36 37#ifndef LIBELFBITS 38# define LIBELFBITS 32 39#endif 40 41 42ElfW2(LIBELFBITS,Chdr) * 43elfw2(LIBELFBITS,getchdr) (Elf_Scn *scn) 44{ 45 ElfW2(LIBELFBITS,Shdr) *shdr = elfw2(LIBELFBITS,getshdr) (scn); 46 if (shdr == NULL) 47 return NULL; 48 49 /* Must have SHF_COMPRESSED flag set. Allocated or no bits sections 50 can never be compressed. */ 51 if ((shdr->sh_flags & SHF_ALLOC) != 0) 52 { 53 __libelf_seterrno (ELF_E_INVALID_SECTION_FLAGS); 54 return NULL; 55 } 56 57 if (shdr->sh_type == SHT_NULL 58 || shdr->sh_type == SHT_NOBITS) 59 { 60 __libelf_seterrno (ELF_E_INVALID_SECTION_TYPE); 61 return NULL; 62 } 63 64 if ((shdr->sh_flags & SHF_COMPRESSED) == 0) 65 { 66 __libelf_seterrno (ELF_E_NOT_COMPRESSED); 67 return NULL; 68 } 69 70 /* This makes sure the data is in the correct format, so we don't 71 need to swap fields. */ 72 Elf_Data *d = elf_getdata (scn, NULL); 73 if (d == NULL) 74 return NULL; 75 76 if (d->d_size < sizeof (ElfW2(LIBELFBITS,Chdr)) || d->d_buf == NULL) 77 { 78 __libelf_seterrno (ELF_E_INVALID_DATA); 79 return NULL; 80 } 81 82 return (ElfW2(LIBELFBITS,Chdr) *) d->d_buf; 83} 84