ecoff_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 2912SN/A */ 3012SN/A 3112SN/A#include <string> 3212SN/A 3356SN/A#include "base/loader/ecoff_object.hh" 342439SN/A#include "base/misc.hh" 3556SN/A#include "base/loader/symtab.hh" 3612SN/A 3756SN/A#include "base/trace.hh" // for DPRINTF 3812SN/A 3956SN/A#include "base/loader/exec_ecoff.h" 4056SN/A#include "base/loader/coff_sym.h" 4156SN/A#include "base/loader/coff_symconst.h" 4212SN/A 4312SN/Ausing namespace std; 4412SN/A 4512SN/AObjectFile * 4612SN/AEcoffObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data) 4712SN/A{ 4812SN/A if (((ecoff_filehdr *)data)->f_magic == ECOFF_MAGIC_ALPHA) { 4912SN/A // it's Alpha ECOFF 50360SN/A return new EcoffObject(fname, fd, len, data, 51360SN/A ObjectFile::Alpha, ObjectFile::Tru64); 5212SN/A } 5312SN/A else { 5412SN/A return NULL; 5512SN/A } 5612SN/A} 5712SN/A 5812SN/A 5912SN/AEcoffObject::EcoffObject(const string &_filename, int _fd, 60360SN/A size_t _len, uint8_t *_data, 61360SN/A Arch _arch, OpSys _opSys) 62360SN/A : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys) 6312SN/A{ 6412SN/A execHdr = (ecoff_exechdr *)fileData; 6512SN/A fileHdr = &(execHdr->f); 6612SN/A aoutHdr = &(execHdr->a); 6712SN/A 6812SN/A entry = aoutHdr->entry; 6912SN/A 7012SN/A text.baseAddr = aoutHdr->text_start; 7112SN/A text.size = aoutHdr->tsize; 722420SN/A text.fileImage = fileData + ECOFF_TXTOFF(execHdr); 7312SN/A 7412SN/A data.baseAddr = aoutHdr->data_start; 7512SN/A data.size = aoutHdr->dsize; 762420SN/A data.fileImage = fileData + ECOFF_DATOFF(execHdr); 7712SN/A 7812SN/A bss.baseAddr = aoutHdr->bss_start; 7912SN/A bss.size = aoutHdr->bsize; 802420SN/A bss.fileImage = NULL; 8112SN/A 8212SN/A DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n", 8312SN/A text.baseAddr, text.size, data.baseAddr, data.size, 8412SN/A bss.baseAddr, bss.size); 8512SN/A} 8612SN/A 8712SN/A 8812SN/Abool 8912SN/AEcoffObject::loadGlobalSymbols(SymbolTable *symtab) 9012SN/A{ 9112SN/A if (!symtab) 9212SN/A return false; 9312SN/A 9412SN/A if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) { 951252SN/A warn("loadGlobalSymbols: wrong magic on %s\n", filename); 9612SN/A return false; 9712SN/A } 9812SN/A 9912SN/A ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr); 10012SN/A if (syms->magic != magicSym2) { 1011252SN/A warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename); 1021252SN/A return false; 10312SN/A } 10412SN/A 10512SN/A ecoff_extsym *ext_syms = (ecoff_extsym *)(fileData + syms->cbExtOffset); 10612SN/A 10712SN/A char *ext_strings = (char *)(fileData + syms->cbSsExtOffset); 10812SN/A for (int i = 0; i < syms->iextMax; i++) { 10912SN/A ecoff_sym *entry = &(ext_syms[i].asym); 11012SN/A if (entry->iss != -1) 11112SN/A symtab->insert(entry->value, ext_strings + entry->iss); 11212SN/A } 11312SN/A 11412SN/A return true; 11512SN/A} 11612SN/A 11712SN/Abool 11812SN/AEcoffObject::loadLocalSymbols(SymbolTable *symtab) 11912SN/A{ 12012SN/A if (!symtab) 12112SN/A return false; 12212SN/A 12312SN/A if (fileHdr->f_magic != ECOFF_MAGIC_ALPHA) { 1241252SN/A warn("loadGlobalSymbols: wrong magic on %s\n", filename); 12512SN/A return false; 12612SN/A } 12712SN/A 12812SN/A ecoff_symhdr *syms = (ecoff_symhdr *)(fileData + fileHdr->f_symptr); 12912SN/A if (syms->magic != magicSym2) { 1301252SN/A warn("loadGlobalSymbols: bad symbol header magic on %s\n", filename); 1311252SN/A return false; 13212SN/A } 13312SN/A 13412SN/A ecoff_sym *local_syms = (ecoff_sym *)(fileData + syms->cbSymOffset); 13512SN/A char *local_strings = (char *)(fileData + syms->cbSsOffset); 13612SN/A ecoff_fdr *fdesc = (ecoff_fdr *)(fileData + syms->cbFdOffset); 13712SN/A 13812SN/A for (int i = 0; i < syms->ifdMax; i++) { 13912SN/A ecoff_sym *entry = (ecoff_sym *)(local_syms + fdesc[i].isymBase); 14012SN/A char *strings = (char *)(local_strings + fdesc[i].issBase); 14112SN/A for (int j = 0; j < fdesc[i].csym; j++) { 14212SN/A if (entry[j].st == stGlobal || entry[j].st == stProc) 14312SN/A if (entry[j].iss != -1) 14412SN/A symtab->insert(entry[j].value, strings + entry[j].iss); 14512SN/A } 14612SN/A } 14712SN/A 14812SN/A for (int i = 0; i < syms->isymMax; i++) { 14912SN/A ecoff_sym *entry = &(local_syms[i]); 15012SN/A if (entry->st == stProc) 15112SN/A symtab->insert(entry->value, local_strings + entry->iss); 15212SN/A } 15312SN/A 15412SN/A return true; 15512SN/A} 156