elf_object.cc revision 2472
12199SN/A/*
22199SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
32199SN/A * All rights reserved.
42199SN/A *
52199SN/A * Redistribution and use in source and binary forms, with or without
62199SN/A * modification, are permitted provided that the following conditions are
72199SN/A * met: redistributions of source code must retain the above copyright
82199SN/A * notice, this list of conditions and the following disclaimer;
92199SN/A * redistributions in binary form must reproduce the above copyright
102199SN/A * notice, this list of conditions and the following disclaimer in the
112199SN/A * documentation and/or other materials provided with the distribution;
122199SN/A * neither the name of the copyright holders nor the names of its
132199SN/A * contributors may be used to endorse or promote products derived from
142199SN/A * this software without specific prior written permission.
152199SN/A *
162199SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172199SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182199SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192199SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202199SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212199SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222199SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232199SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242199SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252199SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262199SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu */
282665Ssaidi@eecs.umich.edu
292665Ssaidi@eecs.umich.edu#include <string>
302665Ssaidi@eecs.umich.edu
312199SN/A// Because of the -Wundef flag we have to do this
322199SN/A#define __LIBELF_INTERNAL__     0
332561SN/A// counterintuitive, but the flag below causes libelf to define
342226SN/A// 64-bit elf types that apparently didn't exist in some older
352561SN/A// versions of Linux.  They seem to be there in 2.4.x, so don't
362199SN/A// set this now (it causes things to break on 64-bit platforms).
372199SN/A#define __LIBELF64_LINUX        0
382680Sktlim@umich.edu#define __LIBELF_NEED_LINK_H    0
392199SN/A#define __LIBELF_SYMBOL_VERSIONS 0
402199SN/A
412199SN/A#include "libelf/libelf.h"
422199SN/A#include "libelf/gelf.h"
432199SN/A
442199SN/A#include "base/loader/elf_object.hh"
452209SN/A#include "base/misc.hh"
462199SN/A
472199SN/A#include "base/loader/symtab.hh"
482458SN/A
492199SN/A#include "base/trace.hh"	// for DPRINTF
502199SN/A
512199SN/A#include "sim/byteswap.hh"
522199SN/A
532199SN/A
544111Sgblack@eecs.umich.eduusing namespace std;
554188Sgblack@eecs.umich.edu
564188Sgblack@eecs.umich.eduObjectFile *
574188Sgblack@eecs.umich.eduElfObject::tryFile(const string &fname, int fd, size_t len, uint8_t *data)
584188Sgblack@eecs.umich.edu{
594188Sgblack@eecs.umich.edu    Elf *elf;
604188Sgblack@eecs.umich.edu    GElf_Ehdr ehdr;
614188Sgblack@eecs.umich.edu    Arch arch = UnknownArch;
624111Sgblack@eecs.umich.edu    OpSys opSys = UnknownOpSys;
634111Sgblack@eecs.umich.edu
644188Sgblack@eecs.umich.edu    // check that header matches library version
654188Sgblack@eecs.umich.edu    if (elf_version(EV_CURRENT) == EV_NONE)
664111Sgblack@eecs.umich.edu        panic("wrong elf version number!");
674111Sgblack@eecs.umich.edu
684111Sgblack@eecs.umich.edu    // get a pointer to elf structure
694111Sgblack@eecs.umich.edu    elf = elf_memory((char*)data,len);
704188Sgblack@eecs.umich.edu    // will only fail if fd is invalid
714188Sgblack@eecs.umich.edu    assert(elf != NULL);
724188Sgblack@eecs.umich.edu
734111Sgblack@eecs.umich.edu    // Check that we actually have a elf file
744111Sgblack@eecs.umich.edu    if (gelf_getehdr(elf, &ehdr) ==0) {
754111Sgblack@eecs.umich.edu        DPRINTFR(Loader, "Not ELF\n");
764111Sgblack@eecs.umich.edu        elf_end(elf);
774111Sgblack@eecs.umich.edu        return NULL;
784111Sgblack@eecs.umich.edu    }
794111Sgblack@eecs.umich.edu    else {
804111Sgblack@eecs.umich.edu        //Detect the architecture
814111Sgblack@eecs.umich.edu        //Versioning issues in libelf need to be resolved to get the correct
824111Sgblack@eecs.umich.edu        //SPARC constants.
834111Sgblack@eecs.umich.edu        //If MIPS supports 32 bit executables, this may need to be changed.
844111Sgblack@eecs.umich.edu        //Also, there are other MIPS constants which may be used, like
854111Sgblack@eecs.umich.edu        //EM_MIPS_RS3_LE and EM_MIPS_X
864111Sgblack@eecs.umich.edu        //Since we don't know how to check for alpha right now, we'll
874111Sgblack@eecs.umich.edu        //just assume if it wasn't something else and it's 64 bit, that's
884111Sgblack@eecs.umich.edu        //what it must be.
894111Sgblack@eecs.umich.edu        if (ehdr.e_machine == EM_SPARC64 ||
904111Sgblack@eecs.umich.edu                ehdr.e_machine == EM_SPARC ||
914111Sgblack@eecs.umich.edu                ehdr.e_machine == EM_SPARCV9) {
924111Sgblack@eecs.umich.edu            arch = ObjectFile::SPARC;
934111Sgblack@eecs.umich.edu        } else if (ehdr.e_machine == EM_MIPS
944111Sgblack@eecs.umich.edu                && ehdr.e_ident[EI_CLASS] == ELFCLASS32) {
954111Sgblack@eecs.umich.edu            arch = ObjectFile::Mips;
964111Sgblack@eecs.umich.edu        } else if (ehdr.e_ident[EI_CLASS] == ELFCLASS64) {
974111Sgblack@eecs.umich.edu            arch = ObjectFile::Alpha;
984111Sgblack@eecs.umich.edu        } else {
994111Sgblack@eecs.umich.edu            arch = ObjectFile::UnknownArch;
1004111Sgblack@eecs.umich.edu        }
1014111Sgblack@eecs.umich.edu
1024111Sgblack@eecs.umich.edu        //Detect the operating system
1034111Sgblack@eecs.umich.edu        switch (ehdr.e_ident[EI_OSABI])
1044111Sgblack@eecs.umich.edu        {
1054111Sgblack@eecs.umich.edu
1064111Sgblack@eecs.umich.edu          case ELFOSABI_LINUX:
1074111Sgblack@eecs.umich.edu            opSys = ObjectFile::Linux;
1084111Sgblack@eecs.umich.edu            break;
1094111Sgblack@eecs.umich.edu          case ELFOSABI_SOLARIS:
1104111Sgblack@eecs.umich.edu            opSys = ObjectFile::Solaris;
1114111Sgblack@eecs.umich.edu            break;
1124111Sgblack@eecs.umich.edu          case ELFOSABI_TRU64:
1134111Sgblack@eecs.umich.edu            opSys = ObjectFile::Tru64;
1144111Sgblack@eecs.umich.edu            break;
1154111Sgblack@eecs.umich.edu          default:
1164111Sgblack@eecs.umich.edu            opSys = ObjectFile::UnknownOpSys;
1174111Sgblack@eecs.umich.edu        }
1184111Sgblack@eecs.umich.edu
1194111Sgblack@eecs.umich.edu        //take a look at the .note.ABI section
1204111Sgblack@eecs.umich.edu        //It can let us know what's what.
1214111Sgblack@eecs.umich.edu        if (opSys == ObjectFile::UnknownOpSys)
1224111Sgblack@eecs.umich.edu        {
1234111Sgblack@eecs.umich.edu            Elf_Scn *section;
1244111Sgblack@eecs.umich.edu            GElf_Shdr shdr;
1254188Sgblack@eecs.umich.edu            Elf_Data *data;
1264111Sgblack@eecs.umich.edu            uint32_t osAbi;;
1274111Sgblack@eecs.umich.edu            int secIdx = 1;
1284111Sgblack@eecs.umich.edu
1294111Sgblack@eecs.umich.edu            // Get the first section
1304111Sgblack@eecs.umich.edu            section = elf_getscn(elf, secIdx);
1314111Sgblack@eecs.umich.edu
1324111Sgblack@eecs.umich.edu            // While there are no more sections
133            while (section != NULL) {
134                gelf_getshdr(section, &shdr);
135                if (shdr.sh_type == SHT_NOTE && !strcmp(".note.ABI-tag",
136                            elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
137                    // we have found a ABI note section
138                    // Check the 5th 32bit word for OS  0 == linux, 1 == hurd,
139                    // 2 == solaris, 3 == freebsd
140                    data = elf_rawdata(section, NULL);
141                    assert(data->d_buf);
142                    if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
143                        osAbi = htole(((uint32_t*)data->d_buf)[4]);
144                    else
145                        osAbi = htobe(((uint32_t*)data->d_buf)[4]);
146
147                    switch(osAbi) {
148                      case 0:
149                        opSys = ObjectFile::Linux;
150                        break;
151                      case 2:
152                        opSys = ObjectFile::Solaris;
153                        break;
154                    }
155                } // if section found
156            section = elf_getscn(elf, ++secIdx);
157            } // while sections
158        }
159
160        int32_t global_ptr;
161        if (arch == ObjectFile::Mips) {
162            Elf_Scn *section;
163            GElf_Shdr shdr;
164            Elf_Data *rdata;
165            int secIdx = 1;
166
167            // Get the first section
168            section = elf_getscn(elf, secIdx);
169
170            // While there are no more sections
171            while (section != NULL) {
172                gelf_getshdr(section, &shdr);
173                /*shdr.sh_type == SHT_MIPS_REGINFO && */
174                if (!strcmp(".reginfo",elf_strptr(elf, ehdr.e_shstrndx, shdr.sh_name))) {
175                    // We have found MIPS reginfo section:
176                    // -------------------------------
177                    // Check the 6th 32bit word for the initialized global pointer value
178                    // -------------------------------
179                    rdata = elf_rawdata(section, NULL);
180                    assert(rdata->d_buf);
181
182                    if(ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
183                        global_ptr = htole(((int32_t*)rdata->d_buf)[5]);
184                    else
185                        global_ptr = htobe(((int32_t*)rdata->d_buf)[5]);
186                    break;
187                }
188
189                section = elf_getscn(elf, ++secIdx);
190            } // if section found
191
192        }
193
194        elf_end(elf);
195        return new ElfObject(fname, fd, len, data, global_ptr,arch, opSys);
196    }
197}
198
199
200ElfObject::ElfObject(const string &_filename, int _fd,
201                     size_t _len, uint8_t *_data,Addr global_ptr,
202                     Arch _arch, OpSys _opSys)
203    : ObjectFile(_filename, _fd, _len, _data, _arch, _opSys)
204
205{
206    Elf *elf;
207    GElf_Ehdr ehdr;
208
209    // check that header matches library version
210    if (elf_version(EV_CURRENT) == EV_NONE)
211        panic("wrong elf version number!");
212
213    // get a pointer to elf structure
214    elf = elf_memory((char*)fileData,len);
215    // will only fail if fd is invalid
216    assert(elf != NULL);
217
218    // Check that we actually have a elf file
219    if (gelf_getehdr(elf, &ehdr) ==0) {
220        panic("Not ELF, shouldn't be here");
221    }
222
223    entry = ehdr.e_entry;
224
225    globalPtr = global_ptr;
226
227    // initialize segment sizes to 0 in case they're not present
228    text.size = data.size = bss.size = 0;
229
230    for (int i = 0; i < ehdr.e_phnum; ++i) {
231        GElf_Phdr phdr;
232        if (gelf_getphdr(elf, i, &phdr) == 0) {
233            panic("gelf_getphdr failed for section %d", i);
234        }
235
236        // for now we don't care about non-loadable segments
237        if (!(phdr.p_type & PT_LOAD))
238            continue;
239
240        // the headers don't explicitly distinguish text from data,
241        // but empirically the text segment comes first.
242        if (text.size == 0) {  // haven't seen text segment yet
243            text.baseAddr = phdr.p_vaddr;
244            text.size = phdr.p_filesz;
245            text.fileImage = fileData + phdr.p_offset;
246            // if there's any padding at the end that's not in the
247            // file, call it the bss.  This happens in the "text"
248            // segment if there's only one loadable segment (as for
249            // kernel images).
250            bss.size = phdr.p_memsz - phdr.p_filesz;
251            bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
252            bss.fileImage = NULL;
253        }
254        else if (data.size == 0) { // have text, this must be data
255            data.baseAddr = phdr.p_vaddr;
256            data.size = phdr.p_filesz;
257            data.fileImage = fileData + phdr.p_offset;
258            // if there's any padding at the end that's not in the
259            // file, call it the bss.  Warn if this happens for both
260            // the text & data segments (should only have one bss).
261            if (phdr.p_memsz - phdr.p_filesz > 0 && bss.size != 0) {
262                warn("Two implied bss segments in file!\n");
263            }
264            bss.size = phdr.p_memsz - phdr.p_filesz;
265            bss.baseAddr = phdr.p_vaddr + phdr.p_filesz;
266            bss.fileImage = NULL;
267        }
268    }
269
270    // should have found at least one loadable segment
271    assert(text.size != 0);
272
273    DPRINTFR(Loader, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
274             text.baseAddr, text.size, data.baseAddr, data.size,
275             bss.baseAddr, bss.size);
276
277    elf_end(elf);
278
279    // We will actually read the sections when we need to load them
280}
281
282
283bool
284ElfObject::loadSomeSymbols(SymbolTable *symtab, int binding)
285{
286    Elf *elf;
287    int sec_idx = 1; // there is a 0 but it is nothing, go figure
288    Elf_Scn *section;
289    GElf_Shdr shdr;
290    Elf_Data *data;
291    int count, ii;
292    bool found = false;
293    GElf_Sym sym;
294
295    if (!symtab)
296        return false;
297
298    // check that header matches library version
299    if (elf_version(EV_CURRENT) == EV_NONE)
300        panic("wrong elf version number!");
301
302    // get a pointer to elf structure
303    elf = elf_memory((char*)fileData,len);
304
305    assert(elf != NULL);
306
307    // Get the first section
308    section = elf_getscn(elf, sec_idx);
309
310    // While there are no more sections
311    while (section != NULL) {
312        gelf_getshdr(section, &shdr);
313
314        if (shdr.sh_type == SHT_SYMTAB) {
315            found = true;
316            data = elf_getdata(section, NULL);
317            count = shdr.sh_size / shdr.sh_entsize;
318            DPRINTF(Loader, "Found Symbol Table, %d symbols present\n", count);
319
320            // loop through all the symbols, only loading global ones
321            for (ii = 0; ii < count; ++ii) {
322                gelf_getsym(data, ii, &sym);
323                if (GELF_ST_BIND(sym.st_info) == binding) {
324                   symtab->insert(sym.st_value,
325                                  elf_strptr(elf, shdr.sh_link, sym.st_name));
326                }
327            }
328        }
329        ++sec_idx;
330        section = elf_getscn(elf, sec_idx);
331    }
332
333    elf_end(elf);
334
335    return found;
336}
337
338bool
339ElfObject::loadGlobalSymbols(SymbolTable *symtab)
340{
341    return loadSomeSymbols(symtab, STB_GLOBAL);
342}
343
344bool
345ElfObject::loadLocalSymbols(SymbolTable *symtab)
346{
347    return loadSomeSymbols(symtab, STB_LOCAL);
348}
349