elf_object.cc revision 2207
1/*
2 * Copyright (c) 2003-2005 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// Because of the -Wundef flag we have to do this
32#define __LIBELF_INTERNAL__     0
33// counterintuitive, but the flag below causes libelf to define
34// 64-bit elf types that apparently didn't exist in some older
35// versions of Linux.  They seem to be there in 2.4.x, so don't
36// set this now (it causes things to break on 64-bit platforms).
37#define __LIBELF64_LINUX        0
38#define __LIBELF_NEED_LINK_H    0
39#define __LIBELF_SYMBOL_VERSIONS 0
40
41#include <libelf/libelf.h>
42#include <libelf/gelf.h>
43
44#include "base/loader/elf_object.hh"
45
46#include "mem/functional/functional.hh"
47#include "base/loader/symtab.hh"
48
49#include "base/trace.hh"	// for DPRINTF
50
51
52using namespace std;
53
54ObjectFile *
55ElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
56{
57    Elf *elf;
58    GElf_Ehdr ehdr;
59    Arch arch = UnknownArch;
60    OpSys opSys = UnknownOpSys;
61
62    // check that header matches library version
63    if (elf_version(EV_CURRENT) == EV_NONE)
64        panic("wrong elf version number!");
65
66    // get a pointer to elf structure
67    elf = elf_memory((char*)data,len);
68    // will only fail if fd is invalid
69    assert(elf != NULL);
70
71    // Check that we actually have a elf file
72    if (gelf_getehdr(elf, &ehdr) ==0) {
73        DPRINTFR(Loader, "Not ELF\n");
74        elf_end(elf);
75        return NULL;
76    }
77    else {
78//        if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
79//            panic("32 bit ELF Binary, Not Supported");
80        /* @todo this emachine value isn't offical yet.
81         *       so we probably shouldn't check it. */
82//        if (ehdr.e_machine != EM_ALPHA)
83//            panic("Non Alpha Binary, Not Supported");
84
85        elf_end(elf);
86
87        //Detect the architecture
88        //Versioning issues in libelf need to be resolved to get the correct
89        //SPARC constants.
90        //If MIPS supports 32 bit executables, this may need to be changed.
91        //Also, there are other MIPS constants which may be used, like
92        //EM_MIPS_RS3_LE and EM_MIPS_X
93        //Since we don't know how to check for alpha right now, we'll
94        //just assume if it wasn't something else and it's 64 bit, that's
95        //what it must be.
96        if (ehdr.e_machine == EM_SPARC64 ||
97                ehdr.e_machine == EM_SPARC ||
98                ehdr.e_machine == EM_SPARCV9) {
99            arch = ObjectFile::SPARC;
100        } else if (ehdr.e_machine == EM_MIPS
101                && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
102            arch = ObjectFile::MIPS;
103        } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
104            arch = ObjectFile::Alpha;
105        } else {
106            arch = ObjectFile::UnknownArch;
107        }
108
109        //Detect the operating system
110        switch (ehdr.e_ident[EI_OSABI])
111        {
112          case ELFOSABI_LINUX:
113            opSys = ObjectFile::Linux;
114            break;
115          case ELFOSABI_SOLARIS:
116            opSys = ObjectFile::Solaris;
117          case ELFOSABI_TRU64:
118            opSys = ObjectFile::Tru64;
119          default:
120            opSys = ObjectFile::UnknownOpSys;
121        }
122
123        return new ElfObject(fname, fd, len, data, arch, opSys);
124    }
125}
126
127
128ElfObject::ElfObject(const string &_filename, int _fd,
129                     size_t _len, uint8_t *_data,
130                     Arch _arch, OpSys _opSys)
131    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
132
133{
134    Elf *elf;
135    GElf_Ehdr ehdr;
136
137    // check that header matches library version
138    if (elf_version(EV_CURRENT) == EV_NONE)
139        panic("wrong elf version number!");
140
141    // get a pointer to elf structure
142    elf = elf_memory((char*)fileData,len);
143    // will only fail if fd is invalid
144    assert(elf != NULL);
145
146    // Check that we actually have a elf file
147    if (gelf_getehdr(elf, &ehdr) ==0) {
148        panic("Not ELF, shouldn't be here");
149    }
150
151    entry = ehdr.e_entry;
152
153    // initialize segment sizes to 0 in case they're not present
154    text.size = data.size = bss.size = 0;
155
156    for (int i = 0; i < ehdr.e_phnum; ++i) {
157        GElf_Phdr phdr;
158        if (gelf_getphdr(elf, i, &phdr) == 0) {
159            panic("gelf_getphdr failed for section %d", i);
160        }
161
162        // for now we don't care about non-loadable segments
163        if (!(phdr.p_type & PT_LOAD))
164            continue;
165
166        // the headers don't explicitly distinguish text from data,
167        // but empirically the text segment comes first.
168        if (text.size == 0) {  // haven't seen text segment yet
169            text.baseAddr = phdr.p_vaddr;
170            text.size = phdr.p_filesz;
171            // remember where the data is for loadSections()
172            fileTextBits = fileData + phdr.p_offset;
173            // if there's any padding at the end that's not in the
174            // file, call it the bss.  This happens in the "text"
175            // segment if there's only one loadable segment (as for
176            // kernel images).
177            bss.size = phdr.p_memsz - phdr.p_filesz;
178            bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
179        }
180        else if (data.size == 0) { // have text, this must be data
181            data.baseAddr = phdr.p_vaddr;
182            data.size = phdr.p_filesz;
183            // remember where the data is for loadSections()
184            fileDataBits = fileData + phdr.p_offset;
185            // if there's any padding at the end that's not in the
186            // file, call it the bss.  Warn if this happens for both
187            // the text & data segments (should only have one bss).
188            if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) {
189                warn("Two implied bss segments in file!\n");
190            }
191            bss.size = phdr.p_memsz - phdr.p_filesz;
192            bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
193        }
194    }
195
196    // should have found at least one loadable segment
197    assert(text.size != 0);
198
199    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
200             text.baseAddr, text.size, data.baseAddr, data.size,
201             bss.baseAddr, bss.size);
202
203    elf_end(elf);
204
205    // We will actually read the sections when we need to load them
206}
207
208
209bool
210ElfObject::loadSections(FunctionalMemory *mem, bool loadPhys)
211{
212    Addr textAddr = text.baseAddr;
213    Addr dataAddr = data.baseAddr;
214
215    if (loadPhys) {
216        textAddr &= (ULL(1) << 40) - 1;
217        dataAddr &= (ULL(1) << 40) - 1;
218    }
219
220    // Since we don't really have an MMU and all memory is
221    // zero-filled, there's no need to set up the BSS segment.
222    if (text.size != 0)
223        mem->prot_write(textAddr, fileTextBits, text.size);
224    if (data.size != 0)
225        mem->prot_write(dataAddr, fileDataBits, data.size);
226
227    return true;
228}
229
230
231bool
232ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding)
233{
234    Elf *elf;
235    int sec_idx = 1; // there is a 0 but it is nothing, go figure
236    Elf_Scn *section;
237    GElf_Shdr shdr;
238    Elf_Data *data;
239    int count, ii;
240    bool found = false;
241    GElf_Sym sym;
242
243    if (!symtab)
244        return false;
245
246    // check that header matches library version
247    if (elf_version(EV_CURRENT) == EV_NONE)
248        panic("wrong elf version number!");
249
250    // get a pointer to elf structure
251    elf = elf_memory((char*)fileData,len);
252
253    assert(elf != NULL);
254
255    // Get the first section
256    section = elf_getscn(elf, sec_idx);
257
258    // While there are no more sections
259    while (section != NULL) {
260        gelf_getshdr(section, &shdr);
261
262        if (shdr.sh_type == SHT_SYMTAB) {
263            found = true;
264            data = elf_getdata(section, NULL);
265            count = shdr.sh_size / shdr.sh_entsize;
266            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
267
268            // loop through all the symbols, only loading global ones
269            for (ii = 0; ii < count; ++ii) {
270                gelf_getsym(data, ii, &sym);
271                if (GELF_ST_BIND(sym.st_info) == binding) {
272                   symtab->insert(sym.st_value,
273                                  elf_strptr(elf, shdr.sh_link, sym.st_name));
274                }
275            }
276        }
277        ++sec_idx;
278        section = elf_getscn(elf, sec_idx);
279    }
280
281    elf_end(elf);
282
283    return found;
284}
285
286bool
287ElfObject::loadGlobalSymbols(SymbolTable *symtab)
288{
289    return loadSomeSymbols(symtab, STB_GLOBAL);
290}
291
292bool
293ElfObject::loadLocalSymbols(SymbolTable *symtab)
294{
295    return loadSomeSymbols(symtab, STB_LOCAL);
296}
297