Path: blob/main/contrib/elftoolchain/libpe/_libpe.h
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*25* $Id: _libpe.h 3312 2016-01-10 09:23:51Z kaiwang27 $26*/2728#ifndef __LIBPE_H_29#define __LIBPE_H_3031#include <sys/types.h>32#include <sys/queue.h>3334#include "libpe.h"3536#include "_elftc.h"3738typedef struct _PE_SecBuf {39PE_Buffer sb_pb; /* application buffer */40PE_Scn *sb_ps; /* PE_Scn pointer */41unsigned int sb_flags; /* buffer flags */42STAILQ_ENTRY(_PE_SecBuf) sb_next;43} PE_SecBuf;4445struct _PE_Scn {46PE *ps_pe; /* PE descriptor */47PE_SecHdr ps_sh; /* section header */48unsigned int ps_ndx; /* 1-based section index */49unsigned int ps_flags; /* section flags */50unsigned int ps_falign; /* section file alignment */51STAILQ_HEAD(, _PE_SecBuf) ps_b; /* buffer list */52STAILQ_ENTRY(_PE_Scn) ps_next;53};5455struct _PE {56int pe_fd; /* file descriptor */57PE_Cmd pe_cmd; /* open mode */58PE_Object pe_obj; /* PE32/PE32+/COFF */59size_t pe_fsize; /* file size */60unsigned int pe_flags; /* library flags */61PE_DosHdr *pe_dh; /* MS-DOS header */62char *pe_stub; /* MS-DOS stub */63size_t pe_stub_ex; /* MS-DOS stub len (exclude hdr) */64char *pe_stub_app; /* MS-DOS stub (app supplied) */65size_t pe_stub_app_sz; /* MS-DOS stub len (app supplied) */66PE_RichHdr *pe_rh; /* rich header */67char *pe_rh_start; /* pointer to rich header */68PE_CoffHdr *pe_ch; /* COFF header */69PE_OptHdr *pe_oh; /* optional header */70PE_DataDir *pe_dd; /* data directories */71unsigned int pe_nscn; /* num. of sections */72char *pe_symtab; /* COFF symbol table */73size_t pe_symbtab_sz; /* size of symbol table */74unsigned int pe_nsym; /* num. of symbols */75unsigned int pe_rvamax; /* maximum RVA */76STAILQ_HEAD(, _PE_Scn) pe_scn; /* section list */77};7879/* Library internal flags */80#define LIBPE_F_API_MASK 0x000FFFU81#define LIBPE_F_SPECIAL_FILE 0x001000U82#define LIBPE_F_BAD_DOS_HEADER 0x002000U83#define LIBPE_F_BAD_PE_HEADER 0x004000U84#define LIBPE_F_BAD_COFF_HEADER 0x008000U85#define LIBPE_F_BAD_OPT_HEADER 0x010000U86#define LIBPE_F_BAD_SEC_HEADER 0x020000U87#define LIBPE_F_LOAD_DOS_STUB 0x040000U88#define LIBPE_F_FD_DONE 0x080000U89#define LIBPE_F_DIRTY_DOS_HEADER 0x100000U90#define LIBPE_F_DIRTY_COFF_HEADER 0x200000U91#define LIBPE_F_DIRTY_OPT_HEADER 0x400000U92#define LIBPE_F_DIRTY_SEC_HEADER 0x800000U9394/* Internal section flags */95#define LIBPE_F_LOAD_SECTION 0x1000U96#define LIBPE_F_STRIP_SECTION 0x2000U9798/* Internal buffer flags */99#define LIBPE_F_BUFFER_MALLOCED 0x1000U100101/* Library internal defines */102#define PE_DOS_MAGIC 0x5a4dU103#define PE_RICH_TEXT "Rich"104#define PE_RICH_HIDDEN 0x536e6144U /* DanS */105#define PE_SIGNATURE 0x4550U /* PE\0\0 */106#define PE_COFF_OPT_SIZE_32 224107#define PE_COFF_OPT_SIZE_32P 240108#define PE_SYM_ENTRY_SIZE 18109110/* Encode/Decode macros */111#if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)112static __inline uint16_t113le16dec(const void *pp)114{115unsigned char const *p = (unsigned char const *)pp;116117return ((p[1] << 8) | p[0]);118}119120static __inline uint32_t121le32dec(const void *pp)122{123unsigned char const *p = (unsigned char const *)pp;124125return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);126}127128static __inline uint64_t129le64dec(const void *pp)130{131unsigned char const *p = (unsigned char const *)pp;132133return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));134}135136static __inline void137le16enc(void *pp, uint16_t u)138{139unsigned char *p = (unsigned char *)pp;140141p[0] = u & 0xff;142p[1] = (u >> 8) & 0xff;143}144145static __inline void146le32enc(void *pp, uint32_t u)147{148unsigned char *p = (unsigned char *)pp;149150p[0] = u & 0xff;151p[1] = (u >> 8) & 0xff;152p[2] = (u >> 16) & 0xff;153p[3] = (u >> 24) & 0xff;154}155156static __inline void157le64enc(void *pp, uint64_t u)158{159unsigned char *p = (unsigned char *)pp;160161le32enc(p, (uint32_t)(u & 0xffffffffU));162le32enc(p + 4, (uint32_t)(u >> 32));163}164#endif /* ELFTC_NEED_BYTEORDER_EXTENSIONS */165166#define PE_READ16(p,v) do { \167(v) = le16dec((p)); \168(p) += 2; \169} while(0)170171#define PE_READ32(p,v) do { \172(v) = le32dec((p)); \173(p) += 4; \174} while(0)175176#define PE_WRITE16(p,v) do { \177le16enc((p), (v)); \178(p) += 2; \179} while(0)180181#define PE_WRITE32(p,v) do { \182le32enc((p), (v)); \183(p) += 4; \184} while(0)185186187/* Internal function declarations */188off_t libpe_align(PE *, off_t, size_t);189PE_SecBuf *libpe_alloc_buffer(PE_Scn *, size_t);190PE_Scn *libpe_alloc_scn(PE *);191int libpe_load_all_sections(PE *);192int libpe_load_section(PE *, PE_Scn *);193int libpe_open_object(PE *);194int libpe_pad(PE *, size_t);195int libpe_parse_msdos_header(PE *, char *);196int libpe_parse_coff_header(PE *, char *);197int libpe_parse_rich_header(PE *);198int libpe_parse_section_headers(PE *);199int libpe_read_msdos_stub(PE *);200void libpe_release_buffer(PE_SecBuf *);201void libpe_release_object(PE *);202void libpe_release_scn(PE_Scn *);203size_t libpe_resync_buffers(PE_Scn *);204int libpe_resync_sections(PE *, off_t);205int libpe_write_buffers(PE_Scn *);206off_t libpe_write_coff_header(PE *, off_t);207off_t libpe_write_msdos_stub(PE *, off_t);208off_t libpe_write_pe_header(PE *, off_t);209off_t libpe_write_sections(PE *, off_t);210off_t libpe_write_section_headers(PE *, off_t);211212#endif /* !__LIBPE_H_ */213214215