Path: blob/main/contrib/elftoolchain/libpe/libpe_init.c
39483 views
/*-1* Copyright (c) 2015 Kai Wang2* 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, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/stat.h>27#include <assert.h>28#include <errno.h>29#include <stdlib.h>30#include <unistd.h>3132#include "_libpe.h"3334ELFTC_VCSID("$Id: libpe_init.c 3312 2016-01-10 09:23:51Z kaiwang27 $");3536int37libpe_open_object(PE *pe)38{39struct stat sb;40mode_t mode;41char magic[sizeof(PE_DosHdr)];4243if (fstat(pe->pe_fd, &sb) < 0)44return (-1);4546mode = sb.st_mode;47pe->pe_fsize = (size_t) sb.st_size;4849/* Reject unsupported file types. */50if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&51!S_ISSOCK(mode)) {52errno = EINVAL;53return (-1);54}5556/* Read/Write mode is not supported for non-regular file. */57if (pe->pe_cmd == PE_C_RDWR && !S_ISREG(mode)) {58errno = EINVAL;59return (-1);60}6162/* The minimal file should at least contain a COFF header. */63if (S_ISREG(mode) && pe->pe_fsize < sizeof(PE_CoffHdr)) {64errno = ENOENT;65return (-1);66}6768/*69* Search for MS-DOS header or COFF header.70*/7172if (read(pe->pe_fd, magic, 2) != 2) {73errno = EIO;74return (-1);75}7677if (magic[0] == 'M' && magic[1] == 'Z') {78pe->pe_obj = PE_O_PE32;79if (read(pe->pe_fd, &magic[2], sizeof(PE_DosHdr) - 2) !=80(ssize_t) sizeof(PE_DosHdr) - 2) {81errno = EIO;82return (-1);83}84return (libpe_parse_msdos_header(pe, magic));8586} else if (magic[0] == 'P' && magic[1] == 'E') {87if (read(pe->pe_fd, magic, 2) != 2) {88errno = EIO;89return (-1);90}91if (magic[0] == '\0' && magic[1] == '\0') {92pe->pe_obj = PE_O_PE32;93if (read(pe->pe_fd, magic, sizeof(PE_CoffHdr)) !=94(ssize_t) sizeof(PE_CoffHdr)) {95errno = EIO;96return (-1);97}98return (libpe_parse_coff_header(pe, magic));99}100errno = ENOENT;101return (-1);102103} else {104pe->pe_obj = PE_O_COFF;105if (read(pe->pe_fd, &magic[2], sizeof(PE_CoffHdr) - 2) !=106(ssize_t) sizeof(PE_CoffHdr) - 2) {107errno = EIO;108return (-1);109}110return (libpe_parse_coff_header(pe, magic));111}112}113114void115libpe_release_object(PE *pe)116{117PE_Scn *ps, *_ps;118119if (pe->pe_dh)120free(pe->pe_dh);121122if (pe->pe_rh) {123free(pe->pe_rh->rh_compid);124free(pe->pe_rh->rh_cnt);125free(pe->pe_rh);126}127128if (pe->pe_ch)129free(pe->pe_ch);130131if (pe->pe_oh)132free(pe->pe_oh);133134if (pe->pe_dd)135free(pe->pe_dd);136137if (pe->pe_stub)138free(pe->pe_stub);139140STAILQ_FOREACH_SAFE(ps, &pe->pe_scn, ps_next, _ps)141libpe_release_scn(ps);142143free(pe);144}145146147