OpenCores
URL https://opencores.org/ocsvn/fwrisc/fwrisc/trunk

Subversion Repositories fwrisc

[/] [fwrisc/] [trunk/] [ve/] [fwrisc/] [tests/] [ElfFileReader.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mballance
/*
2
 * ElfFileReader.cpp
3
 *
4
 * Copyright 2018 Matthew Ballance
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in
8
 * compliance with the License.  You may obtain a copy of
9
 * the License at
10
 *
11
 * http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in
14
 * writing, software distributed under the License is
15
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
16
 * CONDITIONS OF ANY KIND, either express or implied.  See
17
 * the License for the specific language governing
18
 * permissions and limitations under the License.
19
 *
20
 *  Created on: Nov 17, 2018
21
 *      Author: ballance
22
 */
23
 
24
#include "ElfFileReader.h"
25
#include <stdio.h>
26
#include <elf.h>
27
 
28
ElfFileReader::ElfFileReader() {
29
        m_fp = 0;
30
}
31
 
32
ElfFileReader::~ElfFileReader() {
33
        // TODO Auto-generated destructor stub
34
}
35
 
36
bool ElfFileReader::read(const std::string &path) {
37
        Elf32_Phdr phdr;
38
        Elf32_Shdr shdr;
39
 
40
        m_fp = fopen(path.c_str(), "rb");
41
 
42
        if (!m_fp) {
43
                fprintf(stdout, "Error: failed to open file %s\n", path.c_str());
44
                return false;
45
        }
46
 
47
        fread(&m_hdr, sizeof(Elf32_Ehdr), 1, m_fp);
48
 
49
        for (uint32_t i=0; i<m_hdr.e_shnum; i++) {
50
                read(m_hdr.e_shoff+m_hdr.e_shentsize*i, &shdr, sizeof(Elf32_Shdr));
51
 
52
                visit_shdr(shdr);
53
        }
54
 
55
        fclose(m_fp);
56
        m_fp = 0;
57
 
58
        return true;
59
}
60
 
61
void ElfFileReader::read(uint32_t off, void *dst, uint32_t sz) {
62
        fseek(m_fp, off, 0);
63
        fread(dst, sz, 1, m_fp);
64
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.