1295484Semaste/*-
2295484Semaste * Copyright (c) 2016 Kai Wang
3295484Semaste * All rights reserved.
4295484Semaste *
5295484Semaste * Redistribution and use in source and binary forms, with or without
6295484Semaste * modification, are permitted provided that the following conditions
7295484Semaste * are met:
8295484Semaste * 1. Redistributions of source code must retain the above copyright
9295484Semaste *    notice, this list of conditions and the following disclaimer.
10295484Semaste * 2. Redistributions in binary form must reproduce the above copyright
11295484Semaste *    notice, this list of conditions and the following disclaimer in the
12295484Semaste *    documentation and/or other materials provided with the distribution.
13295484Semaste *
14295484Semaste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15295484Semaste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16295484Semaste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17295484Semaste * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18295484Semaste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19295484Semaste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20295484Semaste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21295484Semaste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22295484Semaste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23295484Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24295484Semaste * SUCH DAMAGE.
25295484Semaste */
26295484Semaste
27295484Semaste#include <assert.h>
28295484Semaste#include <errno.h>
29295484Semaste#include <unistd.h>
30295484Semaste
31295484Semaste#include "_libpe.h"
32295484Semaste
33295484SemasteELFTC_VCSID("$Id: pe_update.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
34295484Semaste
35295484Semasteoff_t
36295484Semastepe_update(PE *pe)
37295484Semaste{
38295484Semaste	off_t off;
39295484Semaste
40295484Semaste	if (pe == NULL) {
41295484Semaste		errno = EINVAL;
42295484Semaste		return (-1);
43295484Semaste	}
44295484Semaste
45295484Semaste	if (pe->pe_cmd == PE_C_READ || pe->pe_flags & LIBPE_F_FD_DONE) {
46295484Semaste		errno = EACCES;
47295484Semaste		return (-1);
48295484Semaste	}
49295484Semaste
50295484Semaste	if (pe->pe_cmd == PE_C_RDWR || (pe->pe_cmd == PE_C_WRITE &&
51295484Semaste		(pe->pe_flags & LIBPE_F_SPECIAL_FILE) == 0)) {
52295484Semaste		if (lseek(pe->pe_fd, 0, SEEK_SET) < 0) {
53295484Semaste			errno = EIO;
54295484Semaste			return (-1);
55295484Semaste		}
56295484Semaste	}
57295484Semaste
58295484Semaste	off = 0;
59295484Semaste
60295484Semaste	if (pe->pe_obj == PE_O_PE32 || pe->pe_obj == PE_O_PE32P) {
61295484Semaste		if ((off = libpe_write_msdos_stub(pe, off)) < 0)
62295484Semaste			return (-1);
63295484Semaste
64295484Semaste		if ((off = libpe_write_pe_header(pe, off)) < 0)
65295484Semaste			return (-1);
66295484Semaste	}
67295484Semaste
68295484Semaste	if (libpe_resync_sections(pe, off) < 0)
69295484Semaste		return (-1);
70295484Semaste
71295484Semaste	if ((off = libpe_write_coff_header(pe, off)) < 0)
72295484Semaste		return (-1);
73295484Semaste
74295484Semaste	if ((off = libpe_write_section_headers(pe, off)) < 0)
75295484Semaste		return (-1);
76295484Semaste
77295484Semaste	if ((off = libpe_write_sections(pe, off)) < 0)
78295484Semaste		return (-1);
79295484Semaste
80295484Semaste	if (ftruncate(pe->pe_fd, off) < 0) {
81295484Semaste		errno = EIO;
82295484Semaste		return (-1);
83295484Semaste	}
84295484Semaste
85295484Semaste	return (off);
86295484Semaste}
87