/*-
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by UCHIYAMA Yasushi.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
// read program header
sz = _eh.e_phnum * _eh.e_phentsize;
if ((_ph = static_cast<Elf_Phdr *>(malloc(sz))) == NULL) {
DPRINTF((TEXT("can't allocate program header table.\n")));
return FALSE;
}
if (_file->read(_ph, sz, _eh.e_phoff) != sz) {
DPRINTF((TEXT("program header read error.\n")));
return FALSE;
}
return TRUE;
}
size_t
ElfLoader::memorySize()
{
int i;
Elf_Phdr *ph = _ph;
size_t sz = 0;
size_t extra = 0;
DPRINTF((TEXT("file size: ")));
for (i = 0; i < _eh.e_phnum; i++, ph++) {
if (ph->p_type == PT_LOAD) {
size_t filesz = ph->p_filesz;
DPRINTF((TEXT("%s0x%x"),
sz == 0 ? "" : "+",
filesz));
sz += _mem->roundPage(filesz);
// compensate for partial last tag
extra += _mem->getTaggedPageSize();
if (filesz < ph->p_memsz)
// compensate for zero clear
extra += _mem->getTaggedPageSize();
}
}
// reserve for symbols
size_t symblk_sz = symbol_block_size();
if (symblk_sz) {
sz += symblk_sz;
DPRINTF((TEXT(" = 0x%x]"), symblk_sz));
// XXX: compensate for partial tags after ELF header and symtab
extra += 2 * _mem->getTaggedPageSize();
}