elf_object.cc revision 2665
112SN/A/*
21762SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
312SN/A * All rights reserved.
412SN/A *
512SN/A * Redistribution and use in source and binary forms, with or without
612SN/A * modification, are permitted provided that the following conditions are
712SN/A * met: redistributions of source code must retain the above copyright
812SN/A * notice, this list of conditions and the following disclaimer;
912SN/A * redistributions in binary form must reproduce the above copyright
1012SN/A * notice, this list of conditions and the following disclaimer in the
1112SN/A * documentation and/or other materials provided with the distribution;
1212SN/A * neither the name of the copyright holders nor the names of its
1312SN/A * contributors may be used to endorse or promote products derived from
1412SN/A * this software without specific prior written permission.
1512SN/A *
1612SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1712SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1812SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1912SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2012SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2112SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2212SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2312SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2412SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2512SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2612SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Steve Reinhardt
292665Ssaidi@eecs.umich.edu *          Ali Saidi
3012SN/A */
3112SN/A
3212SN/A#include <string>
3312SN/A
34468SN/A// Because of the -Wundef flag we have to do this
35468SN/A#define __LIBELF_INTERNAL__     0
36468SN/A#define __LIBELF_NEED_LINK_H    0
37661SN/A#define __LIBELF_SYMBOL_VERSIONS 0
38468SN/A
392634Sstever@eecs.umich.edu#include "gelf.h"
40468SN/A
4156SN/A#include "base/loader/elf_object.hh"
422439SN/A#include "base/misc.hh"
4312SN/A
4456SN/A#include "base/loader/symtab.hh"
4512SN/A
4656SN/A#include "base/trace.hh"	// for DPRINTF
4712SN/A
482423SN/A#include "sim/byteswap.hh"
492423SN/A
5012SN/A
5112SN/Ausing namespace std;
5212SN/A
5312SN/AObjectFile *
5412SN/AElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
5512SN/A{
56443SN/A    Elf *elf;
57443SN/A    GElf_Ehdr ehdr;
582207SN/A    Arch arch = UnknownArch;
592207SN/A    OpSys opSys = UnknownOpSys;
60443SN/A
61468SN/A    // check that header matches library version
621708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
631708SN/A        panic("wrong elf version number!");
64443SN/A
65468SN/A    // get a pointer to elf structure
66443SN/A    elf = elf_memory((char*)data,len);
67468SN/A    // will only fail if fd is invalid
68443SN/A    assert(elf != NULL);
69443SN/A
70468SN/A    // Check that we actually have a elf file
71468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
72443SN/A        DPRINTFR(Loader, "Not ELF\n");
73443SN/A        elf_end(elf);
74443SN/A        return NULL;
752476SN/A    } else {
762207SN/A        //Detect the architecture
772207SN/A        //Since we don't know how to check for alpha right now, we'll
782207SN/A        //just assume if it wasn't something else and it's 64 bit, that's
792207SN/A        //what it must be.
802207SN/A        if (ehdr.e_machine == EM_SPARC64 ||
812207SN/A                ehdr.e_machine == EM_SPARC ||
822620SN/A                ehdr.e_machine == EM_SPARCV9) {
832207SN/A            arch = ObjectFile::SPARC;
842207SN/A        } else if (ehdr.e_machine == EM_MIPS
852207SN/A                && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
862472SN/A            arch = ObjectFile::Mips;
872207SN/A        } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
882207SN/A            arch = ObjectFile::Alpha;
892207SN/A        } else {
902600SN/A            warn("Unknown architecture: %d\n", ehdr.e_machine);
912207SN/A            arch = ObjectFile::UnknownArch;
922207SN/A        }
932207SN/A
942207SN/A        //Detect the operating system
952207SN/A        switch (ehdr.e_ident[EI_OSABI])
962207SN/A        {
972238SN/A
982207SN/A          case ELFOSABI_LINUX:
992207SN/A            opSys = ObjectFile::Linux;
1002207SN/A            break;
1012207SN/A          case ELFOSABI_SOLARIS:
1022207SN/A            opSys = ObjectFile::Solaris;
1032238SN/A            break;
1042207SN/A          case ELFOSABI_TRU64:
1052207SN/A            opSys = ObjectFile::Tru64;
1062238SN/A            break;
1072207SN/A          default:
1082207SN/A            opSys = ObjectFile::UnknownOpSys;
1092207SN/A        }
1102207SN/A
1112238SN/A        //take a look at the .note.ABI section
1122238SN/A        //It can let us know what's what.
1132600SN/A        if (opSys == ObjectFile::UnknownOpSys) {
1142238SN/A            Elf_Scn *section;
1152238SN/A            GElf_Shdr shdr;
1162238SN/A            Elf_Data *data;
1172238SN/A            uint32_t osAbi;;
1182238SN/A            int secIdx = 1;
1192238SN/A
1202238SN/A            // Get the first section
1212238SN/A            section = elf_getscn(elf, secIdx);
1222238SN/A
1232238SN/A            // While there are no more sections
1242600SN/A            while (section != NULL && opSys == ObjectFile::UnknownOpSys) {
1252238SN/A                gelf_getshdr(section, &shdr);
1262238SN/A                if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
1272238SN/A                            elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
1282238SN/A                    // we have found a ABI note section
1292238SN/A                    // Check the 5th 32bit word for OS  0 == linux, 1 == hurd,
1302238SN/A                    // 2 == solaris, 3 == freebsd
1312238SN/A                    data = elf_rawdata(section, NULL);
1322238SN/A                    assert(data->d_buf);
1332238SN/A                    if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
1342238SN/A                        osAbi = htole(((uint32_t*)data->d_buf)[4]);
1352238SN/A                    else
1362238SN/A                        osAbi = htobe(((uint32_t*)data->d_buf)[4]);
1372238SN/A
1382238SN/A                    switch(osAbi) {
1392238SN/A                      case 0:
1402238SN/A                        opSys = ObjectFile::Linux;
1412238SN/A                        break;
1422238SN/A                      case 2:
1432238SN/A                        opSys = ObjectFile::Solaris;
1442238SN/A                        break;
1452238SN/A                    }
1462238SN/A                } // if section found
1472600SN/A                if (!strcmp(".SUNW_version", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1482600SN/A                        opSys = ObjectFile::Solaris;
1492600SN/A                if (!strcmp(".stab.index", elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name)))
1502600SN/A                        opSys = ObjectFile::Solaris;
1512600SN/A
1522238SN/A            section = elf_getscn(elf, ++secIdx);
1532238SN/A            } // while sections
1542238SN/A        }
1552472SN/A
1562238SN/A        elf_end(elf);
1572207SN/A        return new ElfObject(fname, fd, len, data, arch, opSys);
15812SN/A    }
15912SN/A}
16012SN/A
16112SN/A
16212SN/AElfObject::ElfObject(const string &_filename, int _fd,
163360SN/A                     size_t _len, uint8_t *_data,
164360SN/A                     Arch _arch, OpSys _opSys)
165360SN/A    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
166443SN/A
16712SN/A{
168443SN/A    Elf *elf;
169443SN/A    GElf_Ehdr ehdr;
17012SN/A
171468SN/A    // check that header matches library version
1721708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
1731708SN/A        panic("wrong elf version number!");
17412SN/A
175468SN/A    // get a pointer to elf structure
176443SN/A    elf = elf_memory((char*)fileData,len);
177468SN/A    // will only fail if fd is invalid
178443SN/A    assert(elf != NULL);
17912SN/A
180468SN/A    // Check that we actually have a elf file
181468SN/A    if (gelf_getehdr(elf, &ehdr) ==0) {
182443SN/A        panic("Not ELF, shouldn't be here");
18312SN/A    }
18412SN/A
185468SN/A    entry = ehdr.e_entry;
18612SN/A
1872472SN/A
188468SN/A    // initialize segment sizes to 0 in case they're not present
189468SN/A    text.size = data.size = bss.size = 0;
190468SN/A
191468SN/A    for (int i = 0; i < ehdr.e_phnum; ++i) {
192468SN/A        GElf_Phdr phdr;
193468SN/A        if (gelf_getphdr(elf, i, &phdr) == 0) {
194468SN/A            panic("gelf_getphdr failed for section %d", i);
195468SN/A        }
196468SN/A
197468SN/A        // for now we don't care about non-loadable segments
198468SN/A        if (!(phdr.p_type & PT_LOAD))
199468SN/A            continue;
200468SN/A
201468SN/A        // the headers don't explicitly distinguish text from data,
202468SN/A        // but empirically the text segment comes first.
203468SN/A        if (text.size == 0) {  // haven't seen text segment yet
204468SN/A            text.baseAddr = phdr.p_vaddr;
205468SN/A            text.size = phdr.p_filesz;
2062420SN/A            text.fileImage = fileData + phdr.p_offset;
207468SN/A            // if there's any padding at the end that's not in the
208468SN/A            // file, call it the bss.  This happens in the "text"
209468SN/A            // segment if there's only one loadable segment (as for
210468SN/A            // kernel images).
211468SN/A            bss.size = phdr.p_memsz - phdr.p_filesz;
212468SN/A            bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
2132420SN/A            bss.fileImage = NULL;
2142476SN/A        } else if (data.size == 0) { // have text, this must be data
215468SN/A            data.baseAddr = phdr.p_vaddr;
216468SN/A            data.size = phdr.p_filesz;
2172420SN/A            data.fileImage = fileData + phdr.p_offset;
218468SN/A            // if there's any padding at the end that's not in the
219468SN/A            // file, call it the bss.  Warn if this happens for both
220468SN/A            // the text & data segments (should only have one bss).
221468SN/A            if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) {
222468SN/A                warn("Two implied bss segments in file!\n");
223468SN/A            }
224468SN/A            bss.size = phdr.p_memsz - phdr.p_filesz;
225468SN/A            bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
2262420SN/A            bss.fileImage = NULL;
2272476SN/A        } else {
2282476SN/A            warn("More than two loadable segments in ELF object.");
2292476SN/A            warn("Ignoring segment @ 0x%x length 0x%x.",
2302476SN/A                 phdr.p_vaddr, phdr.p_filesz);
231468SN/A        }
232468SN/A    }
233468SN/A
234468SN/A    // should have found at least one loadable segment
235468SN/A    assert(text.size != 0);
236468SN/A
237468SN/A    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
238468SN/A             text.baseAddr, text.size, data.baseAddr, data.size,
239468SN/A             bss.baseAddr, bss.size);
240468SN/A
241443SN/A    elf_end(elf);
242443SN/A
243468SN/A    // We will actually read the sections when we need to load them
24412SN/A}
24512SN/A
24612SN/A
24712SN/Abool
248468SN/AElfObject::loadSomeSymbols(SymbolTable *symtab, int binding)
24912SN/A{
250443SN/A    Elf *elf;
251766SN/A    int sec_idx = 1; // there is a 0 but it is nothing, go figure
252443SN/A    Elf_Scn *section;
253443SN/A    GElf_Shdr shdr;
254443SN/A    Elf_Data *data;
255443SN/A    int count, ii;
256443SN/A    bool found = false;
257443SN/A    GElf_Sym sym;
258443SN/A
259443SN/A    if (!symtab)
260443SN/A        return false;
261443SN/A
262468SN/A    // check that header matches library version
2631708SN/A    if (elf_version(EV_CURRENT) == EV_NONE)
2641708SN/A        panic("wrong elf version number!");
265443SN/A
266468SN/A    // get a pointer to elf structure
267443SN/A    elf = elf_memory((char*)fileData,len);
268443SN/A
269443SN/A    assert(elf != NULL);
270443SN/A
271468SN/A    // Get the first section
272454SN/A    section = elf_getscn(elf, sec_idx);
273443SN/A
274468SN/A    // While there are no more sections
275468SN/A    while (section != NULL) {
276443SN/A        gelf_getshdr(section, &shdr);
277443SN/A
278468SN/A        if (shdr.sh_type == SHT_SYMTAB) {
279443SN/A            found = true;
280443SN/A            data = elf_getdata(section, NULL);
281443SN/A            count = shdr.sh_size / shdr.sh_entsize;
282443SN/A            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
283443SN/A
284468SN/A            // loop through all the symbols, only loading global ones
285468SN/A            for (ii = 0; ii < count; ++ii) {
286443SN/A                gelf_getsym(data, ii, &sym);
287836SN/A                if (GELF_ST_BIND(sym.st_info) == binding) {
288468SN/A                   symtab->insert(sym.st_value,
289468SN/A                                  elf_strptr(elf, shdr.sh_link, sym.st_name));
290443SN/A                }
291443SN/A            }
292443SN/A        }
293454SN/A        ++sec_idx;
294454SN/A        section = elf_getscn(elf, sec_idx);
295443SN/A    }
296443SN/A
297443SN/A    elf_end(elf);
298443SN/A
299443SN/A    return found;
30012SN/A}
30112SN/A
30212SN/Abool
303468SN/AElfObject::loadGlobalSymbols(SymbolTable *symtab)
304468SN/A{
305468SN/A    return loadSomeSymbols(symtab, STB_GLOBAL);
306468SN/A}
307468SN/A
308468SN/Abool
30912SN/AElfObject::loadLocalSymbols(SymbolTable *symtab)
31012SN/A{
311468SN/A    return loadSomeSymbols(symtab, STB_LOCAL);
31212SN/A}
313