ecoff_object.cc revision 2420
114039Sstacze01@arm.com/*
214039Sstacze01@arm.com * Copyright (c) 2003-2005 The Regents of The University of Michigan
314039Sstacze01@arm.com * All rights reserved.
414039Sstacze01@arm.com *
514039Sstacze01@arm.com * Redistribution and use in source and binary forms, with or without
614039Sstacze01@arm.com * modification, are permitted provided that the following conditions are
714039Sstacze01@arm.com * met: redistributions of source code must retain the above copyright
814039Sstacze01@arm.com * notice, this list of conditions and the following disclaimer;
914039Sstacze01@arm.com * redistributions in binary form must reproduce the above copyright
1014039Sstacze01@arm.com * notice, this list of conditions and the following disclaimer in the
1114039Sstacze01@arm.com * documentation and/or other materials provided with the distribution;
1214039Sstacze01@arm.com * neither the name of the copyright holders nor the names of its
1314039Sstacze01@arm.com * contributors may be used to endorse or promote products derived from
1414039Sstacze01@arm.com * this software without specific prior written permission.
1514039Sstacze01@arm.com *
1614039Sstacze01@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1714039Sstacze01@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1814039Sstacze01@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1914039Sstacze01@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2014039Sstacze01@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2114039Sstacze01@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2214039Sstacze01@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2314039Sstacze01@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2414039Sstacze01@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2514039Sstacze01@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2614039Sstacze01@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2714039Sstacze01@arm.com */
2814039Sstacze01@arm.com
2914039Sstacze01@arm.com#include <string>
3014039Sstacze01@arm.com
3114039Sstacze01@arm.com#include "base/loader/ecoff_object.hh"
3214039Sstacze01@arm.com#include "base/loader/symtab.hh"
3314039Sstacze01@arm.com
3414039Sstacze01@arm.com#include "base/trace.hh"	// for DPRINTF
3514039Sstacze01@arm.com
3614039Sstacze01@arm.com#include "base/loader/exec_ecoff.h"
3714039Sstacze01@arm.com#include "base/loader/coff_sym.h"
3814039Sstacze01@arm.com#include "base/loader/coff_symconst.h"
3914039Sstacze01@arm.com
4014039Sstacze01@arm.comusing namespace std;
4114039Sstacze01@arm.com
4214039Sstacze01@arm.comObjectFile *
4314039Sstacze01@arm.comEcoffObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
4414039Sstacze01@arm.com{
4514039Sstacze01@arm.com    if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) {
4614039Sstacze01@arm.com        // it's Alpha ECOFF
4714039Sstacze01@arm.com        return new EcoffObject(fname, fd, len, data,
4814039Sstacze01@arm.com                               ObjectFile::Alpha, ObjectFile::Tru64);
4914039Sstacze01@arm.com    }
5014039Sstacze01@arm.com    else {
5114039Sstacze01@arm.com        return NULL;
5214039Sstacze01@arm.com    }
5314039Sstacze01@arm.com}
5414039Sstacze01@arm.com
5514039Sstacze01@arm.com
5614039Sstacze01@arm.comEcoffObject::EcoffObject(const string &_filename, int _fd,
5714039Sstacze01@arm.com                         size_t _len, uint8_t *_data,
5814039Sstacze01@arm.com                         Arch _arch, OpSys _opSys)
5914039Sstacze01@arm.com    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
6014039Sstacze01@arm.com{
6114039Sstacze01@arm.com    execHdr = (ecoff_exechdr *)fileData;
6214039Sstacze01@arm.com    fileHdr = &(execHdr->f);
6314039Sstacze01@arm.com    aoutHdr = &(execHdr->a);
6414039Sstacze01@arm.com
6514039Sstacze01@arm.com    entry = aoutHdr->entry;
6614039Sstacze01@arm.com
6714039Sstacze01@arm.com    text.baseAddr = aoutHdr->text_start;
6814039Sstacze01@arm.com    text.size = aoutHdr->tsize;
6914039Sstacze01@arm.com    text.fileImage = fileData + ECOFF_TXTOFF(execHdr);
7014039Sstacze01@arm.com
7114039Sstacze01@arm.com    data.baseAddr = aoutHdr->data_start;
7214039Sstacze01@arm.com    data.size = aoutHdr->dsize;
7314039Sstacze01@arm.com    data.fileImage = fileData + ECOFF_DATOFF(execHdr);
7414039Sstacze01@arm.com
7514039Sstacze01@arm.com    bss.baseAddr = aoutHdr->bss_start;
7614039Sstacze01@arm.com    bss.size = aoutHdr->bsize;
7714039Sstacze01@arm.com    bss.fileImage = NULL;
7814039Sstacze01@arm.com
7914039Sstacze01@arm.com    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
8014039Sstacze01@arm.com             text.baseAddr, text.size, data.baseAddr, data.size,
8114039Sstacze01@arm.com             bss.baseAddr, bss.size);
8214039Sstacze01@arm.com}
8314039Sstacze01@arm.com
8414039Sstacze01@arm.com
8514039Sstacze01@arm.combool
8614039Sstacze01@arm.comEcoffObject::loadGlobalSymbols(SymbolTable *symtab)
8714039Sstacze01@arm.com{
8814039Sstacze01@arm.com    if (!symtab)
8914039Sstacze01@arm.com        return false;
9014039Sstacze01@arm.com
9114039Sstacze01@arm.com    if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
9214039Sstacze01@arm.com        warn("loadGlobalSymbols: wrong magic on %s\n", filename);
9314039Sstacze01@arm.com        return false;
9414039Sstacze01@arm.com    }
9514039Sstacze01@arm.com
9614039Sstacze01@arm.com    ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
9714039Sstacze01@arm.com    if (syms->magic != magicSym2) {
9814039Sstacze01@arm.com        warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
9914039Sstacze01@arm.com        return false;
10014039Sstacze01@arm.com    }
10114039Sstacze01@arm.com
10214039Sstacze01@arm.com    ecoff_extsym *ext_syms = (ecoff_extsym *)(fileData + syms->cbExtOffset);
10314039Sstacze01@arm.com
10414039Sstacze01@arm.com    char *ext_strings = (char *)(fileData + syms->cbSsExtOffset);
10514039Sstacze01@arm.com    for (int i = 0; i < syms->iextMax; i++) {
10614039Sstacze01@arm.com        ecoff_sym *entry = &(ext_syms[i].asym);
10714039Sstacze01@arm.com        if (entry->iss != -1)
10814039Sstacze01@arm.com            symtab->insert(entry->value, ext_strings + entry->iss);
10914039Sstacze01@arm.com    }
11014039Sstacze01@arm.com
11114039Sstacze01@arm.com    return true;
11214039Sstacze01@arm.com}
11314039Sstacze01@arm.com
11414039Sstacze01@arm.combool
11514039Sstacze01@arm.comEcoffObject::loadLocalSymbols(SymbolTable *symtab)
11614039Sstacze01@arm.com{
11714039Sstacze01@arm.com    if (!symtab)
11814039Sstacze01@arm.com        return false;
11914039Sstacze01@arm.com
12014039Sstacze01@arm.com    if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) {
12114039Sstacze01@arm.com        warn("loadGlobalSymbols: wrong magic on %s\n", filename);
12214039Sstacze01@arm.com        return false;
12314039Sstacze01@arm.com    }
12414039Sstacze01@arm.com
12514039Sstacze01@arm.com    ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr);
12614039Sstacze01@arm.com    if (syms->magic != magicSym2) {
12714039Sstacze01@arm.com        warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename);
12814039Sstacze01@arm.com        return false;
12914039Sstacze01@arm.com    }
13014039Sstacze01@arm.com
13114039Sstacze01@arm.com    ecoff_sym *local_syms = (ecoff_sym *)(fileData + syms->cbSymOffset);
13214039Sstacze01@arm.com    char *local_strings = (char *)(fileData + syms->cbSsOffset);
13314039Sstacze01@arm.com    ecoff_fdr *fdesc = (ecoff_fdr *)(fileData + syms->cbFdOffset);
13414039Sstacze01@arm.com
13514039Sstacze01@arm.com    for (int i = 0; i < syms->ifdMax; i++) {
13614039Sstacze01@arm.com        ecoff_sym *entry = (ecoff_sym *)(local_syms + fdesc[i].isymBase);
13714039Sstacze01@arm.com        char *strings = (char *)(local_strings + fdesc[i].issBase);
13814039Sstacze01@arm.com        for (int j = 0; j < fdesc[i].csym; j++) {
13914039Sstacze01@arm.com            if (entry[j].st == stGlobal || entry[j].st == stProc)
14014039Sstacze01@arm.com                if (entry[j].iss != -1)
14114039Sstacze01@arm.com                    symtab->insert(entry[j].value, strings + entry[j].iss);
14214039Sstacze01@arm.com        }
14314039Sstacze01@arm.com    }
14414039Sstacze01@arm.com
14514039Sstacze01@arm.com    for (int i = 0; i < syms->isymMax; i++) {
14614039Sstacze01@arm.com        ecoff_sym *entry = &(local_syms[i]);
14714039Sstacze01@arm.com        if (entry->st == stProc)
14814039Sstacze01@arm.com            symtab->insert(entry->value, local_strings + entry->iss);
14914039Sstacze01@arm.com    }
15014039Sstacze01@arm.com
15114039Sstacze01@arm.com    return true;
15214039Sstacze01@arm.com}
15314039Sstacze01@arm.com