📄 fc_binutils.cpp
字号:
int itemcount;
fstream content;
char buffer[SIZE_STRINGBUFFER];
/* 慜抜偺庢傝崬傒張棟 */
if(!attach_textfile(content, filename))
{
/* 僶僀僫儕側傜objcopy傪婲摦偡傞 */
FILE * pipe;
string cmdline;
string srecfile;
/* 僾儘僌儔儉婲摦 : objcopy -F srec 僜乕僗僼傽僀儖柤 僥儞億儔儕僼傽僀儖柤 */
strcpy(buffer, "cfgXXXXXX");
mktemp(buffer);
srecfile.assign(buffer);
cmdline = string(CMD_OBJCOPY) + " -F srec " + filename + " " + srecfile;
pipe = popen(cmdline.c_str(), "r");
if(pipe == NULL)
ExceptionMessage("Error on External program execution \"%\"","奜晹僾儘僌儔儉婲摦偵幐攕 - \"%\"") << cmdline << throwException;
pclose(pipe);
/* 僥儞億儔儕偵揻偄偨寢壥傪僗僩儕乕儉偵庢傝崬傓 */
if(!attach_textfile(content, srecfile.c_str()))
ExceptionMessage("Internal error : objcopy did not create the destination srec file \"%\"","撪晹僄儔乕 : objcopy偑僼傽僀儖\"%\"傪弌椡偟偰偄傑偣傫") << srecfile << throwException;
remove(srecfile.c_str()); //UNIX偱偼奐偄偨傑傑嶍彍偼傗偭偰傕偄偄 (僙僉儏儕僥傿偱偼忢搮庤抜傜偟偄)
}
itemcount = symbol_table.size();
VerboseMessage("Section data loading - \"%\"\n","僙僋僔儑儞僨乕僞偺庢摼 - \"%\"\n") << filename;
/* 儌僩儘乕儔S儗僐乕僪僼傽僀儖偺庢傝崬傒 */
unsigned int address;
map<unsigned long, char *>::iterator storage;
content.seekg(0,ios::beg);
storage = contents.end();
address = 0;
while(!content.eof())
{
char * scope;
char * tail;
unsigned int baseaddress;
content.getline(buffer,SIZE_STRINGBUFFER);
scope = buffer;
if(*scope == '\0')
break;
/*
The general format of an S-record follows:
+-------------------//------------------//-----------------------+
| type | count | address | data | checksum |
+-------------------//------------------//-----------------------+
*/
if(*scope != 'S')
ExceptionMessage("Internal error : Syntax error on parsing Motolora S File","撪晹僄儔乕 : 儌僩儘乕儔S儗僐乕僪夝愅帪僄儔乕").throwException();
scope = buffer + 2;
tail = &buffer[4] + hextodec(scope, 2)*2 - 2; //僠僃僢僋僒儉偺暘偩偗庤慜偵堏摦
switch(buffer[1])
{
case '1':
address = hextodec(scope, 4);
break;
case '2':
address = hextodec(scope, 6);
break;
case '3':
address = hextodec(scope, 8);
break;
case '5':
break;
/* 忋婰埲奜偺僞僀僾偼僨乕僞僼傿乕儖僪傪帩偨側偄 */
default:
continue;
}
/* 1僶僀僩偢偮僶僢僼傽偵庢傝崬傫偱偄偔 */
baseaddress = 0;
while(scope < tail)
{
/* 庢傝崬傒愭僶僢僼傽偺儁乕僕愗懼/惗惉張棟 */
if(storage == contents.end() || (address & ~(SIZE_LOADPAGE-1)) != 0)
{
baseaddress += address & ~(SIZE_LOADPAGE-1);
storage = contents.find(baseaddress);
if(storage == contents.end())
storage = contents.insert(pair<unsigned long, char *>(baseaddress, new char[SIZE_LOADPAGE])).first;
address &= SIZE_LOADPAGE - 1;
}
*(storage->second + address) = hextodec(scope, 2);
++ address;
}
address = baseaddress + address; //S5偵旛偊傞
}
}
/* 娭悢僾儗僼傿僋僗偺帺摦専弌 */
void filecontainer_binutils::check_defaultprefix(void)
{
static const char * candidate_list [] = { "_", NULL }; //僾儗僼傿僋僗偺岓曗
static const char target_symbol[] = "_kernel_tmax_tskid"; //昁偢懚嵼偡傞C偺戝堟曄悢
map<string, unsigned long>::iterator scope;
string symbol;
const char ** candidate;
symbol.assign(target_symbol);
/* 傑偢偼僾儗僼傿僋僗側偟偱挧愴 */
scope = symbol_table.find(symbol);
if(scope == symbol_table.end())
{
/* 僾儗僼傿僋僗岓曗傪摢偵懌偟偰嵞挧愴 */
candidate = candidate_list;
while(*candidate != NULL)
{
symbol = string(*candidate) + target_symbol;
scope = symbol_table.find(symbol);
if(scope != symbol_table.end())
{
symbol_prefix.assign(*candidate);
break;
}
++ candidate;
}
/* 偳傟偵傕傂偭偐偐傜側偄 */
if(*candidate == NULL)
ExceptionMessage("Specified symbol table does not contain JSP symbols (Make sure the file is suitable).","巜掕偝傟偨僼傽僀儖偼JSP偺昁恵僔儞儃儖傪娷傫偱偄側偄偨傔丄張棟傪懕峴偱偒傑偣傫").throwException();
}
if(!symbol_prefix.empty())
VerboseMessage(" Checker takes [%] as the default symbol prefix\n"," 僔儞儃儖僾儗僼傿僋僗 : [%]\n") << symbol_prefix;
}
/* 僄儞僨傿傾儞偺帺摦専弌 */
void filecontainer_binutils::check_defaultendian(void)
{
unsigned int tsknum;
/* 僞僗僋偺悢偑偁傑傝偵傕戝偒偄悢(4096埲忋)偩偭偨傜丄僄儞僨傿傾儞偼媡偲敾抐 */
this->load_variable(&tsknum, sizeof(int), "_kernel_tmax_tskid");
if(tsknum > 0x000fffff)
{
//斀揮
endian = endian == LITTLE ? BIG : LITTLE;
}
}
/*
* attach_module : 懳徾儌僕儏乕儖傪傾僞僢僠偡傞
*/
bool filecontainer_binutils::attach_module(const char * filename)
{
string work;
string::size_type pos;
work.assign(filename);
pos = work.find_first_of(',');
if(pos != string::npos)
{
//嵟弶偺崁栚: 僔儞儃儖傪庢摼偡傞僼傽僀儖柤
work.erase(pos);
load_symbols_withGnuNM(work.c_str());
work.assign(filename).erase(0,pos+1);
obtain_contents(work.c_str());
}else
{
load_symbols_withGnuNM(filename); //nm婲摦
obtain_contents(work.c_str()); //objcopy婲摦
}
//帺摦専弌張棟
check_defaultprefix();
check_defaultendian();
return true;
}
bool filecontainer_binutils::change_endian(void * target, unsigned int size)
{
assert(target != NULL);
assert(size != 0);
//儂僗僩偲僞乕僎僢僩偺僄儞僨傿傾儞偑堦弿側傜曄姺偟側偔偰傕偄偄
if(endian == HOST_ENDIAN)
return true;
/*
* 儊僀儞儖乕僾 : bswap偟側偄偱丄慺捈偵彂偔
*/
char * top, * tail;
top = (char *)target;
tail = (char *)target+size -1;
while(top < tail)
{
*top ^= *tail, *tail ^= *top, *top ^= *tail;
++ top;
-- tail;
}
return true;
}
bool filecontainer_binutils::load_contents(void * dest, unsigned long address, unsigned int size)
{
map<unsigned long, char *>::iterator scope;
char * buffer;
assert(dest != NULL);
/* 僶僢僼傽儁乕僕偺慖戰 */
scope = contents.find(address & ~(SIZE_LOADPAGE-1));
if(scope == contents.end())
ExceptionMessage("Internel error: Memory read with unmapped address","撪晹僄儔乕; 儅僢僾偝傟偰側偄傾僪儗僗傪巊偭偰儊儌儕儕乕僪偑峴傢傟傑偟偨").throwException();
/* 撪梕偺暋幨 */
buffer = scope->second + (address & (SIZE_LOADPAGE-1));
memcpy(dest, buffer, size);
return true;
}
/*
* get_symbol_address : 僔儞儃儖柤偐傜傾僪儗僗抣傪庢摼偡傞
*/
unsigned long filecontainer_binutils::get_symbol_address(const char * symbol)
{
map<string, unsigned long>::iterator scope;
string symbol_name;
//僔儞儃儖柤傪惗惉偡傞 ("_"偲偐偺張棟)
symbol_name = symbol_prefix + symbol;
scope = symbol_table.find(symbol_name);
if(scope == symbol_table.end())
return 0;
return (*scope).second;
}
/*
* get_architecture : 傾乕僉僥僋僠儍柤偺庢摼
*/
const char * filecontainer_binutils::get_architecture(void)
{
//偲傝偁偊偢僞乕僎僢僩柤傪帩偭偰傾乕僉僥僋僠儍柤偵偟偰偍偔丅
// #偳偆偣僶僫乕偵偟偐巊偭偰側偄偟偹 崱偺偲偙傠
if(endian == LITTLE)
return "Little endian target (with GNU/Binutils)";
else
return "Big endian target (with GNU/Binutils)";
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -