Path: blob/main/contrib/elftoolchain/libpe/libpe_section.c
39483 views
/*-1* Copyright (c) 2016 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/param.h>27#include <assert.h>28#include <errno.h>29#include <stdlib.h>30#include <string.h>31#include <unistd.h>3233#include "_libpe.h"3435ELFTC_VCSID("$Id: libpe_section.c 3446 2016-05-03 01:31:17Z emaste $");3637PE_Scn *38libpe_alloc_scn(PE *pe)39{40PE_Scn *ps;4142if ((ps = calloc(1, sizeof(PE_Scn))) == NULL) {43errno = ENOMEM;44return (NULL);45}46STAILQ_INIT(&ps->ps_b);47ps->ps_pe = pe;4849return (ps);50}5152void53libpe_release_scn(PE_Scn *ps)54{55PE *pe;56PE_SecBuf *sb, *_sb;5758assert(ps != NULL);5960pe = ps->ps_pe;6162STAILQ_REMOVE(&pe->pe_scn, ps, _PE_Scn, ps_next);6364STAILQ_FOREACH_SAFE(sb, &ps->ps_b, sb_next, _sb)65libpe_release_buffer(sb);6667free(ps);68}6970static int71cmp_scn(PE_Scn *a, PE_Scn *b)72{7374if (a->ps_sh.sh_addr < b->ps_sh.sh_addr)75return (-1);76else if (a->ps_sh.sh_addr == b->ps_sh.sh_addr)77return (0);78else79return (1);80}8182static void83sort_sections(PE *pe)84{8586if (STAILQ_EMPTY(&pe->pe_scn))87return;8889/* Sort the list of Scn by RVA in ascending order. */90STAILQ_SORT(&pe->pe_scn, _PE_Scn, ps_next, cmp_scn);91}9293int94libpe_parse_section_headers(PE *pe)95{96char tmp[sizeof(PE_SecHdr)], *hdr;97PE_Scn *ps;98PE_SecHdr *sh;99PE_CoffHdr *ch;100PE_DataDir *dd;101int found, i;102103assert(pe->pe_ch != NULL);104105for (i = 0; (uint16_t) i < pe->pe_ch->ch_nsec; i++) {106if (read(pe->pe_fd, tmp, sizeof(PE_SecHdr)) !=107(ssize_t) sizeof(PE_SecHdr)) {108pe->pe_flags |= LIBPE_F_BAD_SEC_HEADER;109return (0);110}111112if ((ps = libpe_alloc_scn(pe)) == NULL)113return (-1);114STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);115ps->ps_ndx = ++pe->pe_nscn; /* Setion index is 1-based */116sh = &ps->ps_sh;117118/*119* Note that the section name won't be NUL-terminated if120* its length happens to be 8.121*/122memcpy(sh->sh_name, tmp, sizeof(sh->sh_name));123hdr = tmp + 8;124PE_READ32(hdr, sh->sh_virtsize);125PE_READ32(hdr, sh->sh_addr);126PE_READ32(hdr, sh->sh_rawsize);127PE_READ32(hdr, sh->sh_rawptr);128PE_READ32(hdr, sh->sh_relocptr);129PE_READ32(hdr, sh->sh_lineptr);130PE_READ16(hdr, sh->sh_nreloc);131PE_READ16(hdr, sh->sh_nline);132PE_READ32(hdr, sh->sh_char);133}134135/*136* For all the data directories that don't belong to any section,137* we create pseudo sections for them to make layout easier.138*/139dd = pe->pe_dd;140if (dd != NULL && dd->dd_total > 0) {141for (i = 0; (uint32_t) i < pe->pe_dd->dd_total; i++) {142if (dd->dd_e[i].de_size == 0)143continue;144found = 0;145STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {146sh = &ps->ps_sh;147if (dd->dd_e[i].de_addr >= sh->sh_addr &&148dd->dd_e[i].de_addr + dd->dd_e[i].de_size <=149sh->sh_addr + sh->sh_virtsize) {150found = 1;151break;152}153}154if (found)155continue;156157if ((ps = libpe_alloc_scn(pe)) == NULL)158return (-1);159STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);160ps->ps_ndx = 0xFFFF0000U | i;161sh = &ps->ps_sh;162sh->sh_rawptr = dd->dd_e[i].de_addr; /* FIXME */163sh->sh_rawsize = dd->dd_e[i].de_size;164}165}166167/*168* Also consider the COFF symbol table as a pseudo section.169*/170ch = pe->pe_ch;171if (ch->ch_nsym > 0) {172if ((ps = libpe_alloc_scn(pe)) == NULL)173return (-1);174STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);175ps->ps_ndx = 0xFFFFFFFFU;176sh = &ps->ps_sh;177sh->sh_rawptr = ch->ch_symptr;178sh->sh_rawsize = ch->ch_nsym * PE_SYM_ENTRY_SIZE;179pe->pe_nsym = ch->ch_nsym;180}181182/* PE file headers initialization is complete if we reach here. */183return (0);184}185186int187libpe_load_section(PE *pe, PE_Scn *ps)188{189PE_SecHdr *sh;190PE_SecBuf *sb;191size_t sz;192char tmp[4];193194assert(pe != NULL && ps != NULL);195assert((ps->ps_flags & LIBPE_F_LOAD_SECTION) == 0);196197sh = &ps->ps_sh;198199/* Allocate a PE_SecBuf struct without buffer for empty sections. */200if (sh->sh_rawsize == 0) {201(void) libpe_alloc_buffer(ps, 0);202ps->ps_flags |= LIBPE_F_LOAD_SECTION;203return (0);204}205206if ((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0) {207if (lseek(pe->pe_fd, (off_t) sh->sh_rawptr, SEEK_SET) < 0) {208errno = EIO;209return (-1);210}211}212213if ((sb = libpe_alloc_buffer(ps, sh->sh_rawsize)) == NULL)214return (-1);215216if (read(pe->pe_fd, sb->sb_pb.pb_buf, sh->sh_rawsize) !=217(ssize_t) sh->sh_rawsize) {218errno = EIO;219return (-1);220}221222if (ps->ps_ndx == 0xFFFFFFFFU) {223/*224* Index 0xFFFFFFFF indicates this section is a pseudo225* section that contains the COFF symbol table. We should226* read in the string table right after it.227*/228if (read(pe->pe_fd, tmp, sizeof(tmp)) !=229(ssize_t) sizeof(tmp)) {230errno = EIO;231return (-1);232}233sz = le32dec(tmp);234235/*236* The minimum value for the size field is 4, which indicates237* there is no string table.238*/239if (sz > 4) {240sz -= 4;241if ((sb = libpe_alloc_buffer(ps, sz)) == NULL)242return (-1);243if (read(pe->pe_fd, sb->sb_pb.pb_buf, sz) !=244(ssize_t) sz) {245errno = EIO;246return (-1);247}248}249}250251ps->ps_flags |= LIBPE_F_LOAD_SECTION;252253return (0);254}255256int257libpe_load_all_sections(PE *pe)258{259PE_Scn *ps;260PE_SecHdr *sh;261unsigned r, s;262off_t off;263char tmp[256];264265/* Calculate the current offset into the file. */266off = 0;267if (pe->pe_dh != NULL)268off += pe->pe_dh->dh_lfanew + 4;269if (pe->pe_ch != NULL)270off += sizeof(PE_CoffHdr) + pe->pe_ch->ch_optsize;271272STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {273if (ps->ps_flags & LIBPE_F_LOAD_SECTION)274continue;275sh = &ps->ps_sh;276277/*278* For special files, we consume the padding in between279* and advance to the section offset.280*/281if (pe->pe_flags & LIBPE_F_SPECIAL_FILE) {282/* Can't go backwards. */283if (off > sh->sh_rawptr) {284errno = EIO;285return (-1);286}287if (off < sh->sh_rawptr) {288r = sh->sh_rawptr - off;289for (; r > 0; r -= s) {290s = r > sizeof(tmp) ? sizeof(tmp) : r;291if (read(pe->pe_fd, tmp, s) !=292(ssize_t) s) {293errno = EIO;294return (-1);295}296}297}298}299300/* Load the section content. */301if (libpe_load_section(pe, ps) < 0)302return (-1);303}304305return (0);306}307308int309libpe_resync_sections(PE *pe, off_t off)310{311PE_Scn *ps;312PE_SecHdr *sh;313size_t falign, nsec;314315/* Firstly, sort all sections by their file offsets. */316sort_sections(pe);317318/* Count the number of sections. */319nsec = 0;320STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {321if (ps->ps_flags & LIBPE_F_STRIP_SECTION)322continue;323if (ps->ps_ndx & 0xFFFF0000U)324continue;325nsec++;326}327pe->pe_nscn = nsec;328329/*330* Calculate the file offset for the first section. (`off' is331* currently pointing to the COFF header.)332*/333off += sizeof(PE_CoffHdr);334if (pe->pe_ch != NULL && pe->pe_ch->ch_optsize > 0)335off += pe->pe_ch->ch_optsize;336else {337switch (pe->pe_obj) {338case PE_O_PE32:339off += PE_COFF_OPT_SIZE_32;340break;341case PE_O_PE32P:342off += PE_COFF_OPT_SIZE_32P;343break;344case PE_O_COFF:345default:346break;347}348}349off += nsec * sizeof(PE_SecHdr);350351/*352* Determine the file alignment for sections.353*/354if (pe->pe_oh != NULL && pe->pe_oh->oh_filealign > 0)355falign = pe->pe_oh->oh_filealign;356else {357/*358* Use the default file alignment defined by the359* PE/COFF specification.360*/361if (pe->pe_obj == PE_O_COFF)362falign = 4;363else364falign = 512;365}366367/*368* Step through each section (and pseduo section) and verify369* alignment constraint and overlapping, make adjustment if need.370*/371pe->pe_rvamax = 0;372STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {373if (ps->ps_flags & LIBPE_F_STRIP_SECTION)374continue;375376sh = &ps->ps_sh;377378if (sh->sh_addr + sh->sh_virtsize > pe->pe_rvamax)379pe->pe_rvamax = sh->sh_addr + sh->sh_virtsize;380381if (ps->ps_ndx & 0xFFFF0000U)382ps->ps_falign = 4;383else384ps->ps_falign = falign;385386off = roundup(off, ps->ps_falign);387388if (off != sh->sh_rawptr)389ps->ps_flags |= PE_F_DIRTY;390391if (ps->ps_flags & PE_F_DIRTY) {392if ((ps->ps_flags & LIBPE_F_LOAD_SECTION) == 0) {393if (libpe_load_section(pe, ps) < 0)394return (-1);395}396sh->sh_rawsize = libpe_resync_buffers(ps);397}398399/*400* Sections only contains uninitialized data should set401* PointerToRawData to zero according to the PE/COFF402* specification.403*/404if (sh->sh_rawsize == 0)405sh->sh_rawptr = 0;406else407sh->sh_rawptr = off;408409off += sh->sh_rawsize;410}411412return (0);413}414415off_t416libpe_write_section_headers(PE *pe, off_t off)417{418char tmp[sizeof(PE_SecHdr)], *hdr;419PE_Scn *ps;420PE_SecHdr *sh;421422if (pe->pe_flags & LIBPE_F_BAD_SEC_HEADER || pe->pe_nscn == 0)423return (off);424425if ((pe->pe_flags & LIBPE_F_DIRTY_SEC_HEADER) == 0) {426off += sizeof(PE_SecHdr) * pe->pe_ch->ch_nsec;427return (off);428}429430STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {431if (ps->ps_flags & LIBPE_F_STRIP_SECTION)432continue;433if (ps->ps_ndx & 0xFFFF0000U)434continue;435if ((pe->pe_flags & LIBPE_F_DIRTY_SEC_HEADER) == 0 &&436(ps->ps_flags & PE_F_DIRTY) == 0)437goto next_header;438439sh = &ps->ps_sh;440441memcpy(tmp, sh->sh_name, sizeof(sh->sh_name));442hdr = tmp + 8;443PE_WRITE32(hdr, sh->sh_virtsize);444PE_WRITE32(hdr, sh->sh_addr);445PE_WRITE32(hdr, sh->sh_rawsize);446PE_WRITE32(hdr, sh->sh_rawptr);447PE_WRITE32(hdr, sh->sh_relocptr);448PE_WRITE32(hdr, sh->sh_lineptr);449PE_WRITE16(hdr, sh->sh_nreloc);450PE_WRITE16(hdr, sh->sh_nline);451PE_WRITE32(hdr, sh->sh_char);452453if (write(pe->pe_fd, tmp, sizeof(PE_SecHdr)) !=454(ssize_t) sizeof(PE_SecHdr)) {455errno = EIO;456return (-1);457}458459next_header:460off += sizeof(PE_SecHdr);461}462463return (off);464}465466off_t467libpe_write_sections(PE *pe, off_t off)468{469PE_Scn *ps;470PE_SecHdr *sh;471472if (pe->pe_flags & LIBPE_F_BAD_SEC_HEADER)473return (off);474475STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {476sh = &ps->ps_sh;477478if (ps->ps_flags & LIBPE_F_STRIP_SECTION)479continue;480481/* Skip empty sections. */482if (sh->sh_rawptr == 0 || sh->sh_rawsize == 0)483continue;484485/*486* Padding between sections. (padding always written487* in case the the section headers or sections are488* moved or shrunk.)489*/490assert(off <= sh->sh_rawptr);491if (off < sh->sh_rawptr)492libpe_pad(pe, sh->sh_rawptr - off);493494if ((ps->ps_flags & PE_F_DIRTY) == 0) {495assert((pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0);496if (lseek(pe->pe_fd,497(off_t) (sh->sh_rawptr + sh->sh_rawsize),498SEEK_SET) < 0) {499errno = EIO;500return (-1);501}502off = sh->sh_rawptr + sh->sh_rawsize;503continue;504}505506off = sh->sh_rawptr;507508if (libpe_write_buffers(ps) < 0)509return (-1);510511off += sh->sh_rawsize;512513ps->ps_flags &= ~PE_F_DIRTY;514}515516return (off);517}518519520