/*1* Copyright (c) Christos Zoulas 2008.2* All Rights Reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice immediately at the beginning of the file, without modification,9* this list of conditions, and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR18* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/26if (nbytes <= sizeof(elfhdr))27return 0;2829u.l = 1;30(void)memcpy(&elfhdr, buf, sizeof elfhdr);31swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[EI_DATA];3233type = elf_getu16(swap, elfhdr.e_type);34notecount = ms->elf_notes_max;35switch (type) {36#ifdef ELFCORE37case ET_CORE:38phnum = elf_getu16(swap, elfhdr.e_phnum);39if (phnum > ms->elf_phnum_max)40return toomany(ms, "program headers", phnum);41flags |= FLAGS_IS_CORE;42if (dophn_core(ms, clazz, swap, fd,43CAST(off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,44CAST(size_t, elf_getu16(swap, elfhdr.e_phentsize)),45fsize, &flags, ¬ecount) == -1)46return -1;47break;48#endif49case ET_EXEC:50case ET_DYN:51phnum = elf_getu16(swap, elfhdr.e_phnum);52if (phnum > ms->elf_phnum_max)53return toomany(ms, "program", phnum);54shnum = elf_getu16(swap, elfhdr.e_shnum);55if (shnum > ms->elf_shnum_max)56return toomany(ms, "section", shnum);57if (dophn_exec(ms, clazz, swap, fd,58CAST(off_t, elf_getu(swap, elfhdr.e_phoff)), phnum,59CAST(size_t, elf_getu16(swap, elfhdr.e_phentsize)),60fsize, shnum, &flags, ¬ecount) == -1)61return -1;62/*FALLTHROUGH*/63case ET_REL:64shnum = elf_getu16(swap, elfhdr.e_shnum);65if (shnum > ms->elf_shnum_max)66return toomany(ms, "section headers", shnum);67if (doshn(ms, clazz, swap, fd,68CAST(off_t, elf_getu(swap, elfhdr.e_shoff)), shnum,69CAST(size_t, elf_getu16(swap, elfhdr.e_shentsize)),70fsize, elf_getu16(swap, elfhdr.e_machine),71CAST(int, elf_getu16(swap, elfhdr.e_shstrndx)),72&flags, ¬ecount) == -1)73return -1;74break;7576default:77break;78}79if (notecount == 0)80return toomany(ms, "notes", ms->elf_notes_max);81return 1;828384