4 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5 * See included license file for license details.
8 #include "SourceFile.h"
9 #include "ELFSourceFile.h"
10 #include "SRecordSourceFile.h"
12 #include "format_string.h"
13 #include "SearchPath.h"
15 using namespace elftosb;
17 //! The supported file types are currently:
19 //! - Motorola S-record files
22 //! Any file that is not picked up by the other subclasses will result in a
23 //! an instance of BinaryDataFile.
25 //! \return An instance of the correct subclass of SourceFile for the given path.
27 //! \exception std::runtime_error Thrown if the file cannot be opened.
29 //! \see elftosb::ELFSourceFile
30 //! \see elftosb::SRecordSourceFile
31 //! \see elftosb::BinarySourceFile
32 SourceFile * SourceFile::openFile(const std::string & path)
34 // Search for file using search paths
35 std::string actualPath;
36 bool found = PathSearcher::getGlobalSearcher().search(path, PathSearcher::kFindFile, true, actualPath);
39 throw std::runtime_error(format_string("unable to find file %s\n", path.c_str()));
42 std::ifstream testStream(actualPath.c_str(), std::ios_base::in | std::ios_base::binary);
43 if (!testStream.is_open())
45 throw std::runtime_error(format_string("failed to open file: %s", actualPath.c_str()));
48 // catch exceptions so we can close the file stream
51 if (ELFSourceFile::isELFFile(testStream))
54 return new ELFSourceFile(actualPath);
56 else if (SRecordSourceFile::isSRecordFile(testStream))
59 return new SRecordSourceFile(actualPath);
62 // treat it as a binary file
64 return new BinarySourceFile(actualPath);
73 SourceFile::SourceFile(const std::string & path)
74 : m_path(path), m_stream()
78 //! The file is closed if it had been left opened.
80 SourceFile::~SourceFile()
88 //! \exception std::runtime_error Raised if the file could not be opened successfully.
89 void SourceFile::open()
92 m_stream = new std::ifstream(m_path.c_str(), std::ios_base::in | std::ios_base::binary);
93 if (!m_stream->is_open())
95 throw std::runtime_error(format_string("failed to open file: %s", m_path.c_str()));
99 void SourceFile::close()
104 m_stream.safe_delete();
107 unsigned SourceFile::getSize()
109 bool wasOpen = isOpen();
110 std::ifstream::pos_type oldPosition;
118 oldPosition = m_stream->tellg();
119 m_stream->seekg(0, std::ios_base::end);
120 unsigned resultSize = m_stream->tellg();
121 m_stream->seekg(oldPosition);
131 //! If the file does not support named sections, or if there is not a
132 //! section with the given name, this method may return NULL.
134 //! This method is just a small wrapper that creates an
135 //! FixedMatcher string matcher instance and uses the createDataSource()
136 //! that takes a reference to a StringMatcher.
137 DataSource * SourceFile::createDataSource(const std::string & section)
139 FixedMatcher matcher(section);
140 return createDataSource(matcher);
143 DataTarget * SourceFile::createDataTargetForEntryPoint()
145 if (!hasEntryPoint())
150 return new ConstantDataTarget(getEntryPointAddress());
153 DataSource * BinarySourceFile::createDataSource()
155 std::istream * fileStream = getStream();
159 fileStream->seekg(0, std::ios_base::end);
160 int length = fileStream->tellg();
163 smart_array_ptr<uint8_t> data = new uint8_t[length];
166 // throw std::bad_alloc();
169 // read entire file into the buffer
170 fileStream->seekg(0, std::ios_base::beg);
171 if (fileStream->read((char *)data.get(), length).bad())
173 throw std::runtime_error(format_string("unexpected end of file: %s", m_path.c_str()));
176 // create the data source. the buffer is copied, so we can dispose of it.
177 return new UnmappedDataSource(data, length);