📄 fc_binutils.cpp
字号:
dest += transfer_size;
size -= transfer_size;
}
}
/* 僔儞儃儖偺傾僪儗僗偺庢摼 */
FileContainer::address_t FileContainerBinutilsImpl::getSymbolAddress(const string & symbol) throw(Exception)
{
string symbolname;
map<string, address_t>::const_iterator scope;
symbolname = symbol_prefix + symbol;
scope = symbol_table.find(symbolname);
if(scope == symbol_table.end())
ExceptionMessage("Unknown symbol '%'","晄柧側僔儞儃儖柤 '%'") << symbol << throwException;
return scope->second;
}
/* 傾乕僉僥僋僠儍柤偺庢摼 */
string FileContainerBinutilsImpl::getArchitecture(void) throw()
{
if(byteorder == LITTLE)
return "Little endian target (with GNU/Binutils)";
else
return "Big endian target (with GNU/Binutils)";
}
}
//---------------------------------------------
#ifdef TESTSUITE
#include "base/coverage_undefs.h"
namespace {
fstream * interceptor_file;
string interceptor_filename;
void interceptor(fstream & file, const string & filename)
{
CHECKPOINT("interceptor");
interceptor_file = &file;
interceptor_filename = filename;
if(filename.compare("textfile") == 0) {
remove(filename.c_str());
file.open(filename.c_str(), ios::out);
file << "text";
file.close();
file.open(filename.c_str(), ios::in|ios::binary);
}
else if(filename.compare("binaryfile") == 0) {
remove(filename.c_str());
file.open(filename.c_str(), ios::out|ios::binary);
file.write("\x1",1);
file.close();
file.open(filename.c_str(), ios::in|ios::binary);
}
}
}
TESTSUITE(main, FileContainerBinutilsImpl)
{
PREDECESSOR("TFileContainer");
SingletonBase::ContextChain chain;
chain.saveContext<RuntimeObjectTable>();
BEGIN_CASE("splitFilename","splitFilename") {
BEGIN_CASE("1","僇儞儅偺慜屻偱愗傟傞") {
string first, second;
splitFilename("a,b", first, second);
TEST_CASE("1","first偺拞恎偼惓偟偄", first.compare("a") == 0);
TEST_CASE("2","second偺拞恎偼惓偟偄", second.compare("b") == 0);
} END_CASE;
BEGIN_CASE("2","僇儞儅偺柍偄堷悢傪梌偊傞偲丄椉曽偵摨偠拞恎偑擖傞") {
string first, second;
splitFilename("abc", first, second);
TEST_CASE("1","first偺拞恎偼惓偟偄", first.compare("abc") == 0);
TEST_CASE("2","second偺拞恎偼惓偟偄", second.compare("abc") == 0);
} END_CASE;
BEGIN_CASE("3","嬻暥帤傪梌偊傞偲椺奜") {
bool result = false;
string first, second;
try { splitFilename("", first, second); } catch(Exception &) { result = true; }
if(!result)
TEST_FAIL;
} END_CASE;
} END_CASE;
BEGIN_CASE("hasBinaryContents","hasBinaryContents") {
BEGIN_CASE("1","僥僉僗僩僼傽僀儖傪怘傢偣傞") {
fstream file("test", ios::out);
file << "This is a sample text file.";
file.close();
file.open("test",ios::in|ios::binary);
TEST_CASE("1","娭悢偼false傪曉偡", !hasBinaryContents(file));
TEST_CASE("2","file偼eof偵払偟偰偄側偄", !file.eof());
file.close();
remove("test");
} END_CASE;
BEGIN_CASE("2","僶僀僫儕僨乕僞傪怘傢偣傞") {
fstream file("test", ios::out|ios::binary);
file << "This is a sample text file.";
file.write("\x0\x1\x2\x3", 4);
file.close();
file.open("test",ios::in|ios::binary);
TEST_CASE("1","娭悢偼true傪曉偡", hasBinaryContents(file));
TEST_CASE("2","file偼eof偵払偟偰偄側偄", !file.eof());
file.close();
remove("test");
} END_CASE;
} END_CASE;
BEGIN_CASE("openTextFile","openTextFile") {
BEGIN_CASE("1","僥僉僗僩僼傽僀儖傪巜掕偡傞") {
TestSuite::clearCheckpoints();
fstream file("test", ios::out);
file << "This is a sample text file.";
file.close();
bool result = true;
try { openTextFile(file, "test", interceptor); } catch(Exception &) { result = false; }
TEST_CASE("1","椺奜偼婲偒側偄", result);
TEST_CASE("2","僼傽僀儖偑奐偐傟偰偄傞", file.is_open());
TEST_CASE("3","interceptor偼僐乕儖偝傟偰偄側偄", !TestSuite::isReached("interceptor"));
string work;
getline(file, work, '\n');
TEST_CASE("4","撉傒弌偝傟偨撪梕偑惓偟偄", work.compare("This is a sample text file.") == 0);
file.close();
remove("test");
} END_CASE;
BEGIN_CASE("2","僶僀僫儕僼傽僀儖傪巜掕偡傞 (interceptor偼僼傽僀儖傪奐偐側偄)") {
TestSuite::clearCheckpoints();
fstream file("test", ios::out|ios::binary);
file.write("\x1", 1);
file.close();
bool result = false;
try { openTextFile(file, "test", interceptor); } catch(Exception &) { result = true; }
TEST_CASE("1","椺奜偑婲偒傞", result);
TEST_CASE("2","僼傽僀儖偑奐偐傟偰偄傞", !file.is_open());
TEST_CASE("3","interceptor偑僐乕儖偝傟偰偄傞", TestSuite::isReached("interceptor"));
TEST_CASE("4","interceptor偺堷悢偑惓偟偄 (file)", interceptor_file == &file);
TEST_CASE("5","interceptor偺堷悢偑惓偟偄", interceptor_filename.compare("test") == 0);
file.close();
remove("test");
} END_CASE;
BEGIN_CASE("3","懚嵼偟側偄僼傽僀儖傪巜掕偡傞") {
TestSuite::clearCheckpoints();
fstream file;
bool result = false;
try { openTextFile(file, "___unknown___", interceptor); } catch(Exception &) { result = true; }
TEST_CASE("1","椺奜偑婲偒傞", result);
TEST_CASE("2","僼傽僀儖偑奐偐傟偰偄側偄", !file.is_open());
TEST_CASE("3","interceptor偑僐乕儖偝傟偰偄側偄", !TestSuite::isReached("interceptor"));
} END_CASE;
BEGIN_CASE("4","interceptor偑僥僉僗僩僼傽僀儖傪惗惉偡傞") {
TestSuite::clearCheckpoints();
fstream file("textfile", ios::out|ios::binary);
file.write("\x1", 1);
file.close();
bool result = true;
try { openTextFile(file, "textfile", interceptor); } catch(Exception &) { result = false; }
TEST_CASE("1","椺奜偼婲偒側偄", result);
TEST_CASE("2","僼傽僀儖偑奐偐傟偰偄傞", file.is_open());
TEST_CASE("3","interceptor偑僐乕儖偝傟偰偄傞", TestSuite::isReached("interceptor"));
TEST_CASE("4","interceptor偺堷悢偑惓偟偄 (file)", interceptor_file == &file);
TEST_CASE("5","interceptor偺堷悢偑惓偟偄", interceptor_filename.compare("textfile") == 0);
string work;
getline(file, work, '\n');
TEST_CASE("4","撉傒弌偝傟偨撪梕偑惓偟偄", work.compare("text") == 0);
file.close();
remove("textfile");
} END_CASE;
BEGIN_CASE("5","interceptor偑僶僀僫儕僼傽僀儖傪惗惉偡傞") {
TestSuite::clearCheckpoints();
fstream file("binaryfile", ios::out|ios::binary);
file.write("\x1", 1);
file.close();
bool result = false;
try { openTextFile(file, "binaryfile", interceptor); } catch(Exception &) { result = true; }
TEST_CASE("1","椺奜偼婲偒傞", result);
TEST_CASE("2","僼傽僀儖偑奐偐傟偰偄側偄", !file.is_open());
TEST_CASE("3","interceptor偑僐乕儖偝傟偰偄傞", TestSuite::isReached("interceptor"));
TEST_CASE("4","interceptor偺堷悢偑惓偟偄 (file)", interceptor_file == &file);
TEST_CASE("5","interceptor偺堷悢偑惓偟偄", interceptor_filename.compare("binaryfile") == 0);
file.close();
remove("binaryfile");
} END_CASE;
} END_CASE;
BEGIN_CASE("hextodec","hextodec") {
const char * letter = "0123456789abcdEFg";
const char * work = letter;
TEST_CASE("1", "愗傝弌偝傟偨抣偑惓偟偄", hextodec(work, 2) == 1);
TEST_CASE("2", "work偑恑傫偱偄傞", work == letter + 2);
TEST_CASE("3", "愗傝弌偝傟偨抣偑惓偟偄", hextodec(work, 4) == 0x2345);
TEST_CASE("4", "work偑恑傫偱偄傞", work == letter + 6);
TEST_CASE("5", "愗傝弌偝傟偨抣偑惓偟偄", hextodec(work, 8) == 0x6789abcd);
TEST_CASE("6", "work偑恑傫偱偄傞", work == letter + 14);
TEST_CASE("7", "愗傝弌偝傟偨抣偑惓偟偄", hextodec(work, 8) == 0xef);
TEST_CASE("8", "work偑'g'偺埵抲偱偲傑傞", *work == 'g');
TEST_CASE("9", "愗傝弌偝傟偨抣偑惓偟偄", hextodec(work, 8) == 0);
TEST_CASE("10", "work偑'g'偺埵抲偱偲傑傞", *work == 'g');
} END_CASE;
BEGIN_CASE("readGnuNmLine","readGnuNmLine") {
fstream file("test", ios::out);
file << "0804aab0 T _kernel_task_initialize\n0805d8a0 B _kernel_tcb_table\n\n0804e560 R _kernel_tinib_table\n U getpid@@GLIBC_2.0\n";
file.close();
address_t address;
string attribute;
string symbolname;
file.open("test", ios::in);
BEGIN_CASE("1","晛捠偺僄儞僩儕偑撉傔傞") {
TEST_CASE("1","娭悢偼惉岟偡傞", readGnuNmLine(file, address, attribute, symbolname));
TEST_CASE("2","address偼惓偟偄", address == 0x0804aab0);
TEST_CASE("3","attribute偼惓偟偄", attribute.compare("T") == 0);
TEST_CASE("4","symbolname偼惓偟偄", symbolname.compare("_kernel_task_initialize") == 0);
} END_CASE;
BEGIN_CASE("2","晛捠偺僄儞僩儕偑撉傔傞 (2)") {
TEST_CASE("1","娭悢偼惉岟偡傞", readGnuNmLine(file, address, attribute, symbolname));
TEST_CASE("2","address偼惓偟偄", address == 0x0805d8a0);
TEST_CASE("3","attribute偼惓偟偄", attribute.compare("B") == 0);
TEST_CASE("4","symbolname偼惓偟偄", symbolname.compare("_kernel_tcb_table") == 0);
} END_CASE;
BEGIN_CASE("3","嬻峴傪撉傒旘偽偟偰師偑撉傔傞") {
TEST_CASE("1","娭悢偼惉岟偡傞", readGnuNmLine(file, address, attribute, symbolname));
TEST_CASE("2","address偼惓偟偄", address == 0x0804e560);
TEST_CASE("3","attribute偼惓偟偄", attribute.compare("R") == 0);
TEST_CASE("4","symbolname偼惓偟偄", symbolname.compare("_kernel_tinib_table") == 0);
} END_CASE;
BEGIN_CASE("4","傾僪儗僗偺柍偄僄儞僩儕偼柍帇偡傞") {
TEST_CASE("1","娭悢偼幐攕偡傞", !readGnuNmLine(file, address, attribute, symbolname));
} END_CASE;
file.close();
remove("test");
} END_CASE;
BEGIN_CASE("loadSymbols","loadSymbols") {
fstream file("test", ios::out);
file << "0804aab0 T _kernel_task_initialize\n0805d8a0 B _kernel_tcb_table\n\n0804e560 R _kernel_tinib_table\n U getpid@@GLIBC_2.0\n";
file.close();
file.open("test",ios::in);
FileContainerBinutilsImpl fcbi;
fcbi.loadSymbols(file);
TEST_CASE("1","撉傒崬傑傟偨僄儞僩儕偺悢偑惓偟偄", fcbi.symbol_table.size() == 3);
TEST_CASE("2","僼傽僀儖偼暵偠傜傟偰偄傞", !file.is_open());
remove("test");
} END_CASE;
BEGIN_CASE("writeByte","writeByte") {
FileContainerBinutilsImpl fcbi;
BEGIN_CASE("1","懚嵼偟側偄儁乕僕傊偺彂偒崬傒") {
TEST_CASE("0", "[慜採] contents偺梫慺悢偼0", fcbi.contents.size() == 0);
fcbi.writeByte(0x100, 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -