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 <errno.h>
28295484Semaste
29295484Semaste#include "_libpe.h"
30295484Semaste
31295484SemasteELFTC_VCSID("$Id: pe_symtab.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
32295484Semaste
33295484Semasteint
34295484Semastepe_update_symtab(PE *pe, char *symtab, size_t sz, unsigned int nsym)
35295484Semaste{
36295484Semaste	PE_Scn *ps;
37295484Semaste	PE_SecBuf *sb;
38295484Semaste	PE_SecHdr *sh;
39295484Semaste
40295484Semaste	if (pe == NULL || symtab == NULL || sz == 0) {
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	/* Remove the old symbol table. */
51295484Semaste	STAILQ_FOREACH(ps, &pe->pe_scn, ps_next) {
52295484Semaste		if (ps->ps_ndx == 0xFFFFFFFFU)
53295484Semaste			libpe_release_scn(ps);
54295484Semaste	}
55295484Semaste
56295484Semaste	/*
57295484Semaste	 * Insert the new symbol table.
58295484Semaste	 */
59295484Semaste
60295484Semaste	if ((ps = libpe_alloc_scn(pe)) == NULL)
61295484Semaste		return (-1);
62295484Semaste
63295484Semaste	STAILQ_INSERT_TAIL(&pe->pe_scn, ps, ps_next);
64295484Semaste	ps->ps_ndx = 0xFFFFFFFFU;
65295484Semaste	ps->ps_flags |= PE_F_DIRTY;
66295484Semaste
67295484Semaste	/*
68295484Semaste	 * Set the symbol table section offset to the maximum to make sure
69295484Semaste	 * that it will be placed in the end of the file during section
70295484Semaste	 * layout.
71295484Semaste	 */
72295484Semaste	sh = &ps->ps_sh;
73295484Semaste	sh->sh_rawptr = 0xFFFFFFFFU;
74295484Semaste	sh->sh_rawsize = sz;
75295484Semaste
76295484Semaste	/* Allocate the buffer. */
77295484Semaste	if ((sb = libpe_alloc_buffer(ps, 0)) == NULL)
78295484Semaste		return (-1);
79295484Semaste	sb->sb_flags |= PE_F_DIRTY;
80295484Semaste	sb->sb_pb.pb_size = sz;
81295484Semaste	sb->sb_pb.pb_buf = symtab;
82295484Semaste
83295484Semaste	pe->pe_nsym = nsym;
84295484Semaste
85295484Semaste	return (0);
86295484Semaste}
87