ccoffformat.cpp
来自「TI tms470 coff 格式文件解析」· C++ 代码 · 共 769 行 · 第 1/2 页
CPP
769 行
if(m_symbol[i].sym_tbl.symbol_name.name==NULL)
{
fseek(fp_coff,m_symbol[i].sym_tbl.symbol_name.addr_name.offset,SEEK_SET);
fread(&string_size,4,1,fp_coff);
m_symbol[i].sym_name = (char*)malloc(string_size);
fread(m_symbol[i].sym_name,string_size,1,fp_coff);
}
}
//load line number table
// load string table
fclose(fp_coff);
m_load_ok = true;
return 0;
}
bool CCOFFV2::IsCoffTMS(void)
{
if(m_coffFileHeader.target_ID!=0x0097)
return false;
return true;
}
COFF_FileHeaderV2* CCOFFV2::GetCoffHeader(void)
{
return (COFF_FileHeaderV2*)&m_coffFileHeader;
}
unsigned short CCOFFV2::GetVersionID(void)
{
return m_coffFileHeader.version_ID;
}
SectionHeader* CCOFFV2::GetSectionHeader(int idx)
{
SectionHeader* ptr = NULL;
return ptr;
}
SymbolTable* CCOFFV2::GetSymbalTable(int idx)
{
SymbolTable *ptr = NULL;
return ptr;
}
bool CCOFFV2::IsSpecialSection(int idx)
{
SectionHeader* sect_hd = GetSectionHeader(idx);
if(strcmp(".text",(char*)sect_hd->section_name.name)==0)
{
return true;
}
if(strcmp(".bss",(char*)sect_hd->section_name.name)==0)
{
return true;
}
if(strcmp(".data",(char*)sect_hd->section_name.name)==0)
{
return true;
}
if(strcmp(".etext",(char*)sect_hd->section_name.name)==0)
{
return true;
}
if(strcmp(".edata",(char*)sect_hd->section_name.name)==0)
{
return true;
}
if(strcmp(".end",(char*)sect_hd->section_name.name)==0)
{
return true;
}
return false;
}
void CCOFFV2::DisplayCoffInfo(void)
{
int i = 0;
SectionHeader * ptrSectHd = NULL;
SymbolTable *ptrSymTbl = NULL;
CoffSection *ptrCofSec = NULL;
CoffSymbol *ptrCofSym = NULL;
unsigned char *ptr_data = NULL;
if(!m_load_ok)
{
return;
}
Disp_CoffHeader(&m_coffFileHeader);
// if exist optional file header then read the optional file header
if(m_coffFileHeader.size_opt_header!=0)
{
Disp_OptionalHeader(m_pOptFileHeader);
}
// allocate memory for storing section headers
if(m_sections==NULL)
{
return ;
}
// read section headers
for(i = 0; i< m_coffFileHeader.num_sect_head; i++)
{
ptrSectHd = &(m_sections[i].sect_hd);
if(ptrSectHd==NULL)
{
return ;
}
Disp_SectionHeader(ptrSectHd);
}
// allocate memory for storing symbal tables
if(m_symbol==NULL)
{
return ;
}
for(i = 0; i< m_coffFileHeader.num_entries_sym_tbl; i++)
{
ptrCofSym =(CoffSymbol*)&(m_symbol[i]);
if(ptrCofSym==NULL)
continue;
ptrSymTbl =(SymbolTable*)&ptrCofSym->sym_tbl;
Disp_SymbolTable(ptrSymTbl);
if(ptrCofSym->aux_tbl)
{
Disp_AuxiliarTableEntries(ptrCofSym->aux_tbl);
}
}
}
bool CCOFFV2::COFF2Bin(char* bin_file)
{
return true;
}
void CCOFFV2::ClearCoffContext(void)
{
int i = 0;
SectionHeader * ptrSectHd = NULL;
SymbolTable* ptrSymTbl = NULL;
CoffSection *ptrCofSec = NULL;
CoffSymbol *ptrCofSym = NULL;
for(i = 0; i< m_coffFileHeader.num_entries_sym_tbl; i++)
{
ptrCofSym = (CoffSymbol*)&m_symbol[i];
if(ptrSymTbl->symbol_name.name==NULL)
{
free(ptrCofSym->sym_name);
ptrCofSym->sym_name=NULL;
}
}
// allocate memory for each section data
for(i = 0; i< m_coffFileHeader.num_sect_head; i++)
{
ptrCofSec = (CoffSection*)&m_sections[i];
if(ptrSectHd->section_name.name==NULL)
{
free(ptrCofSec->sect_name);
ptrCofSec->sect_name=NULL;
}
free(ptrCofSec->sect_data);
ptrCofSec->sect_data = NULL;
free(ptrCofSec->relocate_entry);
ptrCofSec->relocate_entry = NULL;
}
// allocate memory for storing section headers
free(m_sections);
m_sections=NULL;
// allocate memory for storing symbal tables
free(m_symbol);
m_symbol=NULL;
memset(&m_coffFileHeader,0,sizeof(COFF_FileHeaderV2));
free(m_pOptFileHeader);
m_load_ok = false;
}
void CCOFFV2::Disp_CoffHeader(COFF_FileHeaderV2* file_hd)
{
if(!file_hd)
return;
printf("start---%s-----\n","COFF_FileHeaderV2");
printf("unsigned short version_ID:0x%x\n",file_hd->version_ID); //indicates version of COFF file structure
printf("unsigned short num_sect_head:%d\n",file_hd->num_sect_head); //Number_of_section_headers
printf("long int time_date_stamp:0x%x\n",file_hd->time_date_stamp); // indicates when the file was created
printf("long symbol_fp:0x%x\n",file_hd->symbol_fp); //contains the symbol table’s starting address
printf("long num_entries_sym_tbl:%d\n",file_hd->num_entries_sym_tbl); //Number of entries in the symbol table
printf("unsigned short size_opt_header:%d\n",file_hd->size_opt_header);//Number of bytes in the optional header. this field is either 0 or 28; if it is 0, there is no optional file header.
printf("unsigned short flags:0x%x\n",file_hd->flags); // (see Table A?2)
printf("unsigned short target_ID:0x%x\n",file_hd->target_ID); // magic number (0097h) indicates the file can be executed in a TMS470R1x system
printf("end----%s----\n","COFF_FileHeaderV2");
}
void CCOFFV2::Disp_OptionalHeader(OptinalFileHeader* opt_f_hd)
{
if(!opt_f_hd)
return;
printf("start---%s-----\n","OptionalFileheader");
printf("short opt_fhd_magic_num:0x%x\n",opt_f_hd->opt_fhd_magic_num); //Optional file header magic number (0108h)
printf("short version_stamp:0x%x\n",opt_f_hd->version_stamp);
printf("long exe_code_size:%d\n",opt_f_hd->exe_code_size); //Size (in bytes) of executable code
printf("long init_data_size:%d\n",opt_f_hd->init_data_size); //Size (in bytes) of initialized data
printf("long uinit_data_size:%d\n",opt_f_hd->uinit_data_size); //Size (in bytes) of uninitialized data
printf("long entry_point:0x%x\n",opt_f_hd->entry_point);
printf("long exe_code_start_addr:0x%x\n",opt_f_hd->exe_code_start_addr); //Beginning address of executable code
printf("long init_data_start_addr:0x%x\n",opt_f_hd->init_data_start_addr); //Beginning address of initialized data
printf("end----%s----\n","OptionalFileheader");
}
void CCOFFV2::Disp_SectionHeader(SectionHeader* sect_hd)
{
CoffSection* pSec = (CoffSection*)sect_hd;
if(!sect_hd)
return;
printf("start---%s-----\n","SectionHeader");
if(sect_hd->section_name.name)
{
printf("union section_name:%s\n",sect_hd->section_name.name);;
}
else
{
printf("union section_name:0x%x\n",sect_hd->section_name.addr_name.offset);;
printf("union section_name:%s\n",pSec->sect_name);
}
printf(" long physical_addr:0x%x\n",sect_hd->physical_addr);//Section’s physical address
printf(" long virtual_addr:0x%x\n",sect_hd->virtual_addr);//Section’s virtual address
printf(" long sect_size:%d\n",sect_hd->sect_size);//Section size in bytes
printf(" long sect_data_fp:0x%x\n",sect_hd->sect_data_fp);//File pointer to raw data
printf(" long relocat_entries_fp:0x%x\n",sect_hd->relocat_entries_fp);//File pointer to relocation entries
printf(" long line_num_entries_fp:0x%x\n",sect_hd->line_num_entries_fp);//File pointer to line number entries
printf(" unsigned long num_relocat_entries:%d\n",sect_hd->num_relocat_entries);//Number of relocation entries
printf(" unsigned long num_line_number_entries:%d\n",sect_hd->num_line_number_entries);//Number of line number entries
printf(" unsigned long flag:0x%x\n",sect_hd->flag);//Flags (see Table A?5)
printf(" unsigned short reserved:0x%x\n",sect_hd->reserved);
printf(" unsigned short mem_page_num:%d\n",sect_hd->mem_page_num);//Memory page number
printf("end----%s----\n","SectionHeader");
}
void CCOFFV2::Disp_RelocationEntry(RelocationEntry* rel_entry)
{
if(!rel_entry)
return;
printf("start---%s-----\n","RelocationEntry");
printf("long virual_addr_ref:0x%x\n",rel_entry->virual_addr_ref);// Virtual address of the reference
printf(" unsigned short sym_tbl_ind:%d\n",rel_entry->sym_tbl_ind);//Symbol table index (0?65 535)
printf(" unsigned short reserved:0x%x\n",rel_entry->reserved);
printf(" unsigned short relocat_type:0x%x\n",rel_entry->relocat_type);//Relocation type (see Table A?7)
printf("end----%s----\n","RelocationEntry");
}
void CCOFFV2::Disp_SymbolTable(SymbolTable* sym_tbl)
{
CoffSymbol* pSym = (CoffSymbol*)sym_tbl;
if(!sym_tbl)
return;
printf("start---%s-----\n","SymbolTable");
if(sym_tbl->symbol_name.name!=NULL)
{
printf("union symbol_name:%s\n",sym_tbl->symbol_name.name);
}
else
{
printf("union symbol_name:0x%x\n",sym_tbl->symbol_name.addr_name.offset);
printf("union symbol_name:%s\n",sym_tbl->symbol_name);
}
printf(" long symbol_value:0x%x\n",sym_tbl->symbol_value);// storage class dependent
printf(" short sect_num_symbol:%d\n",sym_tbl->sect_num_symbol);//section number of the symbol
printf(" unsigned short bs_derive_type:0x%x\n",sym_tbl->bs_derive_type);// Basic and derived type specification
printf(" char storage_class:0x%x\n",sym_tbl->storage_class);//Character Storage class of the symbol Table A?10. Symbol Storage Classes
printf(" char num_aux_entries:%d\n",sym_tbl->num_aux_entries);//Character Number of auxiliary entries (always 0 or 1)
printf("end----%s----\n","SymbolTable");
}
void CCOFFV2::Disp_AuxiliarTableEntries(AuxiliarTableEntries* aux_tbl_entry)
{
if(!aux_tbl_entry)
return;
printf("start---%s-----\n","AuxiliarTableEntries");
printf(" int sect_len:%d\n",aux_tbl_entry->sect_len);//Integer Section length
printf(" unsigned short num_relocat_entries:%d\n",aux_tbl_entry->num_relocat_entries);//Number of relocation entries
printf(" unsigned short num_line_num_entries:%d\n",aux_tbl_entry->num_line_num_entries);//Number of line number entries
printf(" unsigned char reserved[10]:0x%x\n",aux_tbl_entry->reserved[0]);//— Not used (zero filled)
printf("end----%s----\n","AuxiliarTableEntries");
}
void CCOFFV2::Disp_StringTable(StringTable* str_tbl)
{
if(!str_tbl)
return;
printf("start---%s-----\n","StringTable");
printf("end----%s----\n","StringTable");
}
int main(int argc, char* argv[])
{
CCOFFV2 coff;
if(argc<2)
{
printf("param is two few \n");
return 0;
}
if(coff.LoadCoffFile(argv[1])!=0)
{
printf("coff file load error\n");
return 0;
}
coff.DisplayCoffInfo();
return 0;
}
//-----------------------------------------------
#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?