elf_object.cc revision 360
1/*
2 * Copyright (c) 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <string>
30
31#include "base/loader/elf_object.hh"
32
33#include "mem/functional_mem/functional_memory.hh"
34#include "base/loader/symtab.hh"
35
36#include "base/trace.hh"	// for DPRINTF
37
38#include "base/loader/exec_elf.h"
39
40using namespace std;
41
42ObjectFile *
43ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
44{
45    if (memcmp(((Elf64_Ehdr *)data)->e_ident, ELFMAG, SELFMAG) == 0) {
46        // for now we'll assume it's a 64-bit Alpha Linux binary
47        return new ElfObject(fname, fd, len, data,
48                             ObjectFile::Alpha, ObjectFile::Linux);
49    }
50    else {
51        return NULL;
52    }
53}
54
55
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