object_file.cc revision 2
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 <list> 30#include <string> 31 32#include <sys/types.h> 33#include <sys/mman.h> 34#include <fcntl.h> 35#include <stdio.h> 36#include <unistd.h> 37 38#include "cprintf.hh" 39#include "ecoff.hh" 40#include "object_file.hh" 41#include "symtab.hh" 42 43using namespace std; 44 45ObjectFile::ObjectFile() 46 : descriptor(-1), data(NULL) 47{} 48 49ObjectFile::ObjectFile(string file) 50 : descriptor(-1), data(NULL) 51{ open(file); } 52 53ObjectFile::~ObjectFile() 54{ close(); } 55 56bool 57ObjectFile::open(string file_name) 58{ 59 close(); 60 61 name = file_name; 62 63 descriptor = ::open(name.c_str(), O_RDONLY); 64 if (descriptor < 0) 65 return false; 66 67 len = (size_t)::lseek(descriptor, 0, SEEK_END); 68 69 data = (uint8_t *)::mmap(NULL, len, PROT_READ, MAP_SHARED, descriptor, 0); 70 if (data == MAP_FAILED) 71 return false; 72 73 postOpen(); 74 75 return true; 76} 77 78void 79ObjectFile::close() 80{ 81 if (descriptor >= 0) 82 ::close(descriptor); 83 84 if (data) 85 ::munmap(data, len); 86} 87 88void 89EcoffObject::postOpen() 90{ 91 exec = &(((EcoffExecHeader *)data)->f); 92 aout = &(((EcoffExecHeader *)data)->a); 93 94 text_off = aout->text_start; 95 data_off = aout->data_start; 96 bss_off = aout->bss_start; 97 98 text_size = aout->tsize; 99 data_size = aout->dsize; 100 bss_size = aout->bsize; 101} 102 103bool 104EcoffObject::loadGlobals(SymbolTable *symtab) 105{ 106 if (!symtab) 107 return false; 108 109 if (exec->f_magic != ALPHAMAGIC) { 110 cprintf("wrong magic\n"); 111 return false; 112 } 113 114 EcoffSymHeader *syms = (EcoffSymHeader *)(data + exec->f_symptr); 115 if (syms->magic != ECOFF_SYM_MAGIC) { 116 cprintf("bad symbol header magic\n"); 117 exit(1); 118 } 119 120 EcoffExtSymEntry *ext_syms = 121 (EcoffExtSymEntry *)(data + syms->cbExtOffset); 122 123 char *ext_strings = (char *)(data + syms->cbSsExtOffset); 124 for (int i = 0; i < syms->iextMax; i++) { 125 EcoffSymEntry *entry = &(ext_syms[i].asym); 126 if (entry->iss != -1) 127 symtab->insert(entry->value, ext_strings + entry->iss); 128 } 129 130 return true; 131} 132 133bool 134EcoffObject::loadLocals(SymbolTable *symtab) 135{ 136 if (!symtab) 137 return false; 138 139 if (exec->f_magic != ALPHAMAGIC) { 140 cprintf("wrong magic\n"); 141 return false; 142 } 143 144 EcoffSymHeader *syms = (EcoffSymHeader *)(data + exec->f_symptr); 145 if (syms->magic != ECOFF_SYM_MAGIC) { 146 cprintf("bad symbol header magic\n"); 147 exit(1); 148 } 149 150 EcoffSymEntry *local_syms = (EcoffSymEntry *)(data + syms->cbSymOffset); 151 char *local_strings = (char *)(data + syms->cbSsOffset); 152 EcoffFileDesc *fdesc = (EcoffFileDesc *)(data + syms->cbFdOffset); 153 154 for (int i = 0; i < syms->ifdMax; i++) { 155 EcoffSymEntry *entry = 156 (EcoffSymEntry *)(local_syms + fdesc[i].isymBase); 157 char *strings = (char *)(local_strings + fdesc[i].issBase); 158 for (int j = 0; j < fdesc[i].csym; j++) { 159 if (entry[j].st == 1 || entry[j].st == 6) 160 if (entry[j].iss != -1) 161 symtab->insert(entry[j].value, strings + entry[j].iss); 162 } 163 } 164 165 for (int i = 0; i < syms->isymMax; i++) { 166 EcoffSymEntry *entry = &(local_syms[i]); 167 if (entry->st == 6) 168 if (entry->st == 1 || entry->st == 6) 169 symtab->insert(entry->value, local_strings + entry->iss); 170 } 171 172 return true; 173} 174