2 * Copyright (c) 2003 The Regents of The University of Michigan
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.
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.
31 #include "elf_object.hh"
33 #include "functional_memory.hh"
36 #include "trace.hh" // for DPRINTF
43 ElfObject::tryFile(const string
&fname
, int fd
, size_t len
, uint8_t *data
)
45 if (memcmp(((Elf64_Ehdr
*)data
)->e_ident
, ELFMAG
, SELFMAG
) == 0) {
46 // for now we'll assume it's a 64-bit Alpha binary
47 return new ElfObject(fname
, fd
, len
, data
);
55 ElfObject::ElfObject(const string
&_filename
, int _fd
,
56 size_t _len
, uint8_t *_data
)
57 : ObjectFile(_filename
, _fd
, _len
, _data
)
59 ehdr
= (Elf64_Ehdr
*)fileData
;
61 entry
= ehdr
->e_entry
;
63 phdr
= (Elf64_Phdr
*)(fileData
+ ehdr
->e_phoff
);
64 assert(sizeof(Elf64_Phdr
) == ehdr
->e_phentsize
);
66 bool foundText
= false;
67 bool foundData
= false;
68 for (int i
= 0; i
< ehdr
->e_phnum
; ++i
) {
69 Elf64_Phdr
*p
= &phdr
[i
];
71 // for now we don't care about non-loadable segments
72 if (!(p
->p_type
& PT_LOAD
))
75 if (p
->p_flags
& PF_X
) {
76 // executable: must be text
80 text
.baseAddr
= p
->p_vaddr
;
81 text
.size
= p
->p_filesz
;
82 assert(p
->p_filesz
== p
->p_memsz
);
85 assert(p
->p_flags
& PF_R
);
89 data
.baseAddr
= p
->p_vaddr
;
90 data
.size
= p
->p_filesz
;
91 bss
.baseAddr
= data
.baseAddr
+ data
.size
;
92 bss
.size
= p
->p_memsz
- p
->p_filesz
;
96 assert(foundText
&& foundData
);
98 DPRINTFR(Loader
, "text: 0x%x %d\ndata: 0x%x %d\nbss: 0x%x %d\n",
99 text
.baseAddr
, text
.size
, data
.baseAddr
, data
.size
,
100 bss
.baseAddr
, bss
.size
);
105 ElfObject::loadSections(FunctionalMemory
*mem
, bool loadPhys
)
107 Addr textAddr
= text
.baseAddr
;
108 Addr dataAddr
= data
.baseAddr
;
111 textAddr
&= (ULL(1) << 40) - 1;
112 dataAddr
&= (ULL(1) << 40) - 1;
115 // Since we don't really have an MMU and all memory is
116 // zero-filled, there's no need to set up the BSS segment.
118 mem
->prot_write(textAddr
, fileData
+ phdr
[textPhdrIdx
].p_offset
,
121 mem
->prot_write(dataAddr
, fileData
+ phdr
[dataPhdrIdx
].p_offset
,
129 ElfObject::loadGlobalSymbols(SymbolTable
*symtab
)
131 // symbols not supported yet
136 ElfObject::loadLocalSymbols(SymbolTable
*symtab
)
138 // symbols not supported yet