elf_object.cc revision 360
113219Sodanrc@yahoo.com.br/*
213219Sodanrc@yahoo.com.br * Copyright (c) 2003 The Regents of The University of Michigan
313219Sodanrc@yahoo.com.br * All rights reserved.
413219Sodanrc@yahoo.com.br *
513219Sodanrc@yahoo.com.br * Redistribution and use in source and binary forms, with or without
613219Sodanrc@yahoo.com.br * modification, are permitted provided that the following conditions are
713219Sodanrc@yahoo.com.br * met: redistributions of source code must retain the above copyright
813219Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer;
913219Sodanrc@yahoo.com.br * redistributions in binary form must reproduce the above copyright
1013219Sodanrc@yahoo.com.br * notice, this list of conditions and the following disclaimer in the
1113219Sodanrc@yahoo.com.br * documentation and/or other materials provided with the distribution;
1213219Sodanrc@yahoo.com.br * neither the name of the copyright holders nor the names of its
1313219Sodanrc@yahoo.com.br * contributors may be used to endorse or promote products derived from
1413219Sodanrc@yahoo.com.br * this software without specific prior written permission.
1513219Sodanrc@yahoo.com.br *
1613219Sodanrc@yahoo.com.br * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1713219Sodanrc@yahoo.com.br * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1813219Sodanrc@yahoo.com.br * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1913219Sodanrc@yahoo.com.br * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2013219Sodanrc@yahoo.com.br * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2113219Sodanrc@yahoo.com.br * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2213219Sodanrc@yahoo.com.br * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2313219Sodanrc@yahoo.com.br * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2413219Sodanrc@yahoo.com.br * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2513219Sodanrc@yahoo.com.br * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2613219Sodanrc@yahoo.com.br * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2713219Sodanrc@yahoo.com.br */
2813219Sodanrc@yahoo.com.br
2913219Sodanrc@yahoo.com.br#include <string>
3013219Sodanrc@yahoo.com.br
3113219Sodanrc@yahoo.com.br#include "base/loader/elf_object.hh"
3213219Sodanrc@yahoo.com.br
3313219Sodanrc@yahoo.com.br#include "mem/functional_mem/functional_memory.hh"
3413219Sodanrc@yahoo.com.br#include "base/loader/symtab.hh"
3513219Sodanrc@yahoo.com.br
3613219Sodanrc@yahoo.com.br#include "base/trace.hh"	// for DPRINTF
3713219Sodanrc@yahoo.com.br
3813219Sodanrc@yahoo.com.br#include "base/loader/exec_elf.h"
3913219Sodanrc@yahoo.com.br
4013219Sodanrc@yahoo.com.brusing namespace std;
4113219Sodanrc@yahoo.com.br
4213219Sodanrc@yahoo.com.brObjectFile *
4313219Sodanrc@yahoo.com.brElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
4413219Sodanrc@yahoo.com.br{
4513219Sodanrc@yahoo.com.br    if (memcmp(((Elf64_Ehdr *)data)->e_ident, ELFMAG, SELFMAG) == 0) {
4613219Sodanrc@yahoo.com.br        // for now we'll assume it's a 64-bit Alpha Linux binary
4713219Sodanrc@yahoo.com.br        return new ElfObject(fname, fd, len, data,
4813219Sodanrc@yahoo.com.br                             ObjectFile::Alpha, ObjectFile::Linux);
4913219Sodanrc@yahoo.com.br    }
5013219Sodanrc@yahoo.com.br    else {
5113219Sodanrc@yahoo.com.br        return NULL;
5213219Sodanrc@yahoo.com.br    }
5313219Sodanrc@yahoo.com.br}
5413219Sodanrc@yahoo.com.br
5513219Sodanrc@yahoo.com.br
56ElfObject::ElfObject(const string &_filename, int _fd,
57                     size_t _len, uint8_t *_data,
58                     Arch _arch, OpSys _opSys)
59    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
60{
61    ehdr = (Elf64_Ehdr *)fileData;
62
63    entry = ehdr->e_entry;
64
65    phdr = (Elf64_Phdr *)(fileData + ehdr->e_phoff);
66    assert(sizeof(Elf64_Phdr) == ehdr->e_phentsize);
67
68    bool foundText = false;
69    bool foundData = false;
70    for (int i = 0; i < ehdr->e_phnum; ++i) {
71        Elf64_Phdr *p = &phdr[i];
72
73        // for now we don't care about non-loadable segments
74        if (!(p->p_type & PT_LOAD))
75            continue;
76
77        if (p->p_flags & PF_X) {
78            // executable: must be text
79            assert(!foundText);
80            foundText = true;
81            textPhdrIdx = i;
82            text.baseAddr = p->p_vaddr;
83            text.size = p->p_filesz;
84            assert(p->p_filesz == p->p_memsz);
85        }
86        else {
87            assert(p->p_flags & PF_R);
88            assert(!foundData);
89            foundData = true;
90            dataPhdrIdx = i;
91            data.baseAddr = p->p_vaddr;
92            data.size = p->p_filesz;
93            bss.baseAddr = data.baseAddr + data.size;
94            bss.size = p->p_memsz - p->p_filesz;
95        }
96    }
97
98    assert(foundText && foundData);
99
100    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
101             text.baseAddr, text.size, data.baseAddr, data.size,
102             bss.baseAddr, bss.size);
103}
104
105
106bool
107ElfObject::loadSections(FunctionalMemory *mem, bool loadPhys)
108{
109    Addr textAddr = text.baseAddr;
110    Addr dataAddr = data.baseAddr;
111
112    if (loadPhys) {
113        textAddr &= (ULL(1) << 40) - 1;
114        dataAddr &= (ULL(1) << 40) - 1;
115    }
116
117    // Since we don't really have an MMU and all memory is
118    // zero-filled, there's no need to set up the BSS segment.
119    if (text.size != 0)
120        mem->prot_write(textAddr, fileData + phdr[textPhdrIdx].p_offset,
121                        text.size);
122    if (data.size != 0)
123        mem->prot_write(dataAddr, fileData + phdr[dataPhdrIdx].p_offset,
124                        data.size);
125
126    return true;
127}
128
129
130bool
131ElfObject::loadGlobalSymbols(SymbolTable *symtab)
132{
133    // symbols not supported yet
134    return false;
135}
136
137bool
138ElfObject::loadLocalSymbols(SymbolTable *symtab)
139{
140    // symbols not supported yet
141    return false;
142}
143