本文整理汇总了C++中SgAsmGenericSection类的典型用法代码示例。如果您正苦于以下问题:C++ SgAsmGenericSection类的具体用法?C++ SgAsmGenericSection怎么用?C++ SgAsmGenericSection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SgAsmGenericSection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: ROSE_ASSERT
/** Attaches a previously unattached PE Section to the PE Section Table. This method complements
* SgAsmPESection::init_from_section_table. This method initializes the section table from the section while
* init_from_section_table() initializes the section from the section table. */
void
SgAsmPESectionTable::add_section(SgAsmPESection *section)
{
ROSE_ASSERT(section!=NULL);
ROSE_ASSERT(section->get_file()==get_file());
ROSE_ASSERT(section->get_header()==get_header());
ROSE_ASSERT(section->get_section_entry()==NULL); /* must not be in the section table yet */
SgAsmPEFileHeader *fhdr = dynamic_cast<SgAsmPEFileHeader*>(get_header());
ROSE_ASSERT(fhdr!=NULL);
/* Assign an ID if there isn't one yet. */
if (section->get_id()<0) {
SgAsmGenericSectionList *seclist = fhdr->get_sections();;
int max_id=0; /*assume zero is used so we start at one*/
for (size_t i=0; i<seclist->get_sections().size(); i++) {
SgAsmGenericSection *s = seclist->get_sections()[i];
max_id = std::max(max_id, s->get_id());
}
section->set_id(max_id+1);
}
/* Create a new section table entry. */
SgAsmPESectionTableEntry *entry = new SgAsmPESectionTableEntry;
entry->update_from_section(section);
section->set_section_entry(entry);
}
示例2: ROSE_ASSERT
/* Creates the storage item for the string at the specified offset. If "shared" is true then attempt to re-use a previous storage
* object, otherwise create a new one. Each storage object is considered to be a separate string, therefore when two strings
* share the same storage object, changing one string changes the other. */
SgAsmStringStorage *
SgAsmCoffStrtab::create_storage(rose_addr_t offset, bool shared)
{
ROSE_ASSERT(offset!=SgAsmGenericString::unallocated);
SgAsmGenericSection *container = get_container();
/* Has the string already been created? */
if (shared) {
for (referenced_t::iterator i=p_storage_list.begin(); i!=p_storage_list.end(); ++i) {
if ((*i)->get_offset()==offset && (*i)!=p_dont_free)
return *i;
}
}
/* Read string length byte */
unsigned char byte;
container->read_content_local(offset, &byte, 1);
unsigned len = byte;
/* Make sure new storage isn't inside some other string. (We don't support nested strings in COFF where the length byte of
* the nested string is one of the characters of the outer string.) */
for (referenced_t::iterator i=p_storage_list.begin(); i!=p_storage_list.end(); ++i) {
ROSE_ASSERT((*i)->get_offset()==SgAsmGenericString::unallocated ||
offset + 1 + len <= (*i)->get_offset() ||
offset >= 1 + (*i)->get_string().size());
}
/* Create storage object */
char *buf = new char[len];
container->read_content_local(offset+1, buf, len);
SgAsmStringStorage *storage = new SgAsmStringStorage(this, std::string(buf, len), offset);
delete[] buf;
/* It's a bad idea to free (e.g., modify) strings before we've identified all the strings in the table. Consider
* the case where two strings have the same value and point to the same offset (i.e., they share storage). If we modify one
* before we know about the other then (at best) we modify the other one also.
*
* The only time we can guarantee this is OK is when the new storage points to the same file location as "dont_free"
* since the latter is guaranteed to never be freed or shared. This exception is used when creating a new, unallocated
* string (see SgAsmStoredString(SgAsmGenericStrtab,const std::string&)). */
if (p_num_freed>0 && (!p_dont_free || offset!=p_dont_free->get_offset())) {
fprintf(stderr,
"SgAsmCoffStrtab::create_storage(%"PRIu64"): %zu other string%s (of %zu created) in [%d] \"%s\""
" %s been modified and/or reallocated!\n",
offset, p_num_freed, 1==p_num_freed?"":"s", p_storage_list.size(),
container->get_id(), container->get_name()->get_string(true).c_str(),
1==p_num_freed?"has":"have");
ROSE_ASSERT(0==p_num_freed);
}
set_isModified(true);
p_storage_list.push_back(storage);
return storage;
}
示例3:
/* Same as parent, but also includes sections that aren't mapped but which contain code. */
SgAsmGenericSectionPtrList
BinaryLoaderElfObj::get_remap_sections(SgAsmGenericHeader *header)
{
SgAsmGenericSectionPtrList retval = BinaryLoaderElf::get_remap_sections(header);
const SgAsmGenericSectionPtrList §ions = header->get_sections()->get_sections();
for (SgAsmGenericSectionPtrList::const_iterator si=sections.begin(); si!=sections.end(); ++si) {
SgAsmGenericSection *section = *si;
if (!section->is_mapped() && section->get_contains_code())
retval.push_back(section);
}
return retval;
}
示例4: get_rvasize_pairs
/** Update all the RVA/Size pair info from the section to which it points. */
void
SgAsmPEFileHeader::update_rvasize_pairs()
{
for (size_t i=0; i<get_rvasize_pairs()->get_pairs().size(); i++) {
SgAsmPERVASizePair *pair = get_rvasize_pairs()->get_pairs()[i];
SgAsmGenericSection *section = pair->get_section();
if (section) {
pair->set_e_rva(rose_rva_t(section->get_mapped_preferred_rva(), section));
pair->set_e_size(section->get_mapped_size());
}
}
}
示例5:
/** Returns sections that have a preferred mapping that includes the specified relative virtual address. */
SgAsmGenericSectionPtrList
SgAsmGenericHeader::get_sections_by_rva(rose_addr_t rva) const
{
SgAsmGenericSectionPtrList retval;
for (SgAsmGenericSectionPtrList::iterator i = p_sections->get_sections().begin(); i!=p_sections->get_sections().end(); ++i) {
SgAsmGenericSection *section = *i;
if (section->is_mapped() &&
rva >= section->get_mapped_preferred_rva() && rva < section->get_mapped_preferred_rva() + section->get_mapped_size()) {
retval.push_back(section);
}
}
return retval;
}
示例6: get_container
/** Write string table back to disk. Free space is zeroed out; holes are left as they are. */
void
SgAsmElfStrtab::unparse(std::ostream &f) const
{
SgAsmGenericSection *container = get_container();
/* Write strings with NUL termination. Shared strings will be written more than once, but that's OK. */
for (size_t i=0; i<p_storage_list.size(); i++) {
SgAsmStringStorage *storage = p_storage_list[i];
ROSE_ASSERT(storage->get_offset()!=SgAsmGenericString::unallocated);
rose_addr_t at = container->write(f, storage->get_offset(), storage->get_string());
container->write(f, at, '\0');
}
/* Fill free areas with zero */
BOOST_FOREACH (const AddressInterval &interval, get_freelist().intervals())
container->write(f, interval.least(), std::string(interval.size(), '\0'));
}
示例7: get_container
/* Write string table back to disk. Free space is zeroed out; holes are left as they are. */
void
SgAsmCoffStrtab::unparse(std::ostream &f) const
{
SgAsmGenericSection *container = get_container();
/* Write length coded strings. Shared strings will be written more than once, but that's OK. */
for (size_t i=0; i<p_storage_list.size(); i++) {
SgAsmStringStorage *storage = p_storage_list[i];
ROSE_ASSERT(storage->get_offset()!=SgAsmGenericString::unallocated);
rose_addr_t at = container->write(f, storage->get_offset(), storage->get_string());
container->write(f, at, '\0');
}
/* Fill free areas with zero */
for (ExtentMap::const_iterator i=get_freelist().begin(); i!=get_freelist().end(); ++i) {
container->write(f, i->first.first(), std::string(i->first.size(), '\0'));
}
}
示例8: SgAsmGenericSectionPtrList
/** Returns sections having a preferred or actual mapping that includes the specified virtual address. If @p use_preferred is
* set, then the condition is evaluated by looking at the section's preferred mapping, otherwise the actual mapping is used.
* If an actual mapping is used, the specified virtual address must be part of the actual mapped section, not merely in the
* memory region that was also mapped to satisfy alignment constraints. */
SgAsmGenericSectionPtrList
SgAsmGenericHeader::get_sections_by_va(rose_addr_t va, bool use_preferred) const
{
if (use_preferred) {
if (va < get_base_va())
return SgAsmGenericSectionPtrList();
rose_addr_t rva = va - get_base_va();
return get_sections_by_rva(rva);
}
SgAsmGenericSectionPtrList retval;
for (size_t i=0; i<p_sections->get_sections().size(); i++) {
SgAsmGenericSection *section = p_sections->get_sections()[i];
if (section->is_mapped() &&
va>=section->get_mapped_actual_va() && va<section->get_mapped_actual_va()+section->get_mapped_size())
retval.push_back(section);
}
return retval;
}
示例9: fprintf
/** Class method that prints info about offsets into known sections */
void
SgAsmGenericSection::dump_containing_sections(FILE *f, const std::string &prefix, rose_rva_t rva,
const SgAsmGenericSectionPtrList &slist)
{
for (size_t i=0; i<slist.size(); i++) {
SgAsmGenericSection *s = slist[i];
if (s->is_mapped() && rva>=s->get_mapped_preferred_rva() && rva<s->get_mapped_preferred_rva()+s->get_mapped_size()) {
rose_addr_t offset = rva - s->get_mapped_preferred_rva();
fprintf(f, "%-*s is 0x%08"PRIx64" (%"PRIu64") bytes into section [%d] \"%s\"\n",
DUMP_FIELD_WIDTH, prefix.c_str(), offset, offset, s->get_id(), s->get_name()->get_string(true).c_str());
}
}
}
示例10: isSgAsmFunction
/** Returns the base address of an addressable IR node. */
uint64_t
SgAsmIntegerValueExpression::virtualAddress(SgNode *node)
{
if (!node)
return 0;
if (isSgAsmFunction(node))
return isSgAsmFunction(node)->get_entry_va();
if (isSgAsmStatement(node)) // instructions, block, function, staticdata, ...
return isSgAsmStatement(node)->get_address();
if (isSgAsmGenericSymbol(node))
return isSgAsmGenericSymbol(node)->get_value();
if (isSgAsmPEImportItem(node))
return isSgAsmPEImportItem(node)->get_bound_rva().get_va();
if (isSgAsmPERVASizePair(node))
return isSgAsmPERVASizePair(node)->get_e_rva().get_va();
if (isSgAsmGenericSection(node)) {
SgAsmGenericSection *section = isSgAsmGenericSection(node);
if (section->is_mapped())
return section->get_mapped_actual_va();
return 0;
}
ASSERT_not_reachable("need addressable node type, got " + stringifyVariantT(node->variantT(), "V_"));
}
示例11: sprintf
/* Print some debugging info */
void
SgAsmGenericHeader::dump(FILE *f, const char *prefix, ssize_t idx) const
{
char p[4096];
if (idx>=0) {
sprintf(p, "%sHeader[%zd].", prefix, idx);
} else {
sprintf(p, "%sHeader.", prefix);
}
const int w = std::max(1, DUMP_FIELD_WIDTH-(int)strlen(p));
SgAsmGenericSection::dump(f, p, -1);
ROSE_ASSERT(p_exec_format != NULL);
p_exec_format->dump(f, p, -1);
fprintf(f, "%s%-*s = 0x%x (%s)\n", p, w, "ins_arch",
p_isa, stringifySgAsmExecutableFileFormatInsSetArchitecture(p_isa).c_str());
fprintf(f, "%s%-*s = \"", p, w, "magic");
for (size_t i = 0; i < p_magic.size(); i++) {
switch (p_magic[i]) {
case '\\': fputs("\\\\", f); break;
case '\n': fputs("\\n", f); break;
case '\r': fputs("\\r", f); break;
case '\t': fputs("\\t", f); break;
default:
if (isprint(p_magic[i])) {
fputc(p_magic[i], f);
} else {
fprintf(f, "\\%03o", (unsigned)p_magic[i]);
}
break;
}
}
fputs("\"\n", f);
/* Base virtual address and entry addresses */
fprintf(f, "%s%-*s = 0x%08" PRIx64" (%" PRIu64")\n", p, w, "base_va", get_base_va(), get_base_va());
fprintf(f, "%s%-*s = %" PRIuPTR " entry points\n", p, w, "entry_rva.size", p_entry_rvas.size());
for (size_t i = 0; i < p_entry_rvas.size(); i++) {
char label[64];
sprintf(label, "entry_rva[%" PRIuPTR "]", i);
rose_addr_t entry_rva = p_entry_rvas[i].get_rva();
fprintf(f, "%s%-*s = 0x%08" PRIx64 " (%" PRIu64 ")\n", p, w, label, entry_rva, entry_rva);
SgAsmGenericSectionPtrList sections = get_file()->get_sections();
dump_containing_sections(f, std::string(p)+label, entry_rva, sections);
}
fprintf(f, "%s%-*s = %" PRIuPTR " sections\n", p, w, "section", p_sections->get_sections().size());
for (size_t i=0; i<p_sections->get_sections().size(); i++) {
SgAsmGenericSection *section = p_sections->get_sections()[i];
char label[1024];
sprintf(label, "section[%" PRIuPTR "]", i);
fprintf(f, "%s%-*s = [%d] \"%s\"\n", p, w, label, section->get_id(), section->get_name()->get_string(true).c_str());
}
fprintf(f, "%s%-*s = %" PRIuPTR " entries\n", p, w, "DLL.size", p_dlls->get_dlls().size());
for (size_t i = 0; i < p_dlls->get_dlls().size(); i++)
p_dlls->get_dlls()[i]->dump(f, p, i);
}
示例12: rvasize_pair_name
/* Looks at the RVA/Size pairs in the PE header and creates an SgAsmGenericSection object for each one. This must be done
* after we build the mapping from virtual addresses to file offsets. */
void
SgAsmPEFileHeader::create_table_sections()
{
/* First, only create the sections. */
for (size_t i=0; i<p_rvasize_pairs->get_pairs().size(); i++) {
SgAsmPERVASizePair *pair = p_rvasize_pairs->get_pairs()[i];
if (0==pair->get_e_size())
continue;
/* Table names come from PE file specification and are hard coded by RVA/Size pair index */
const char *tabname_short;
std::string tabname = rvasize_pair_name((PairPurpose)i, &tabname_short);
/* Find the starting offset in the file.
* FIXME: We have a potential problem here in that ROSE sections are always contiguous in the file but a section created
* from an RVA/Size pair is not necessarily contiguous in the file. Normally such sections are in fact
* contiguous and we'll just ignore this for now. In any case, as long as these sections only ever read their
* data via the same MemoryMap that we use here, everything should be fine. [RPM 2009-08-17] */
MemoryMap *map = get_loader_map();
ROSE_ASSERT(map!=NULL);
const MemoryMap::MapElement *elmt = map->find(get_base_va() + pair->get_e_rva());
if (!elmt) {
fprintf(stderr, "SgAsmPEFileHeader::create_table_sections: warning: pair-%zu, rva=0x%08"PRIx64", size=%"PRIu64
" bytes \"%s\": unable to find a mapping for the virtual address (skipping)\n",
i, pair->get_e_rva().get_rva(), pair->get_e_size(), tabname.c_str());
continue;
}
rose_addr_t file_offset = elmt->is_anonymous() ? 0 : elmt->get_va_offset(get_base_va() + pair->get_e_rva(), 1);
/* Create the new section */
SgAsmGenericSection *tabsec = NULL;
switch (i) {
case 0: {
/* Sometimes export sections are represented by a ".edata" section, and sometimes they're represented by an
* RVA/Size pair, and sometimes both point to the same part of the file. We don't want the exports duplicated
* in the AST, so we only create this table as exports if we haven't already seen some other export section. */
SgAsmGenericSectionPtrList §ions = get_sections()->get_sections();
bool seen_exports = false;
for (SgAsmGenericSectionPtrList::iterator si=sections.begin(); !seen_exports && si!=sections.end(); ++si)
seen_exports = isSgAsmPEExportSection(*si);
if (seen_exports) {
tabsec = new SgAsmGenericSection(get_file(), this);
} else {
tabsec = new SgAsmPEExportSection(this);
}
break;
}
case 1: {
/* Sometimes import sections are represented by a ".idata" section, and sometimes they're represented by an
* RVA/Size pair, and sometimes both point to the same part of the file. We don't want the imports duplicated
* in the AST, so we only create this table as imports if we haven't already seen some other import section. */
SgAsmGenericSectionPtrList §ions = get_sections()->get_sections();
bool seen_imports = false;
for (SgAsmGenericSectionPtrList::iterator si=sections.begin(); !seen_imports && si!=sections.end(); ++si)
seen_imports = isSgAsmPEImportSection(*si);
if (seen_imports) {
tabsec = new SgAsmGenericSection(get_file(), this);
} else {
tabsec = new SgAsmPEImportSection(this);
}
break;
}
default: {
tabsec = new SgAsmGenericSection(get_file(), this);
break;
}
}
tabsec->set_name(new SgAsmBasicString(tabname));
tabsec->set_short_name(tabname_short);
tabsec->set_synthesized(true);
tabsec->set_purpose(SP_HEADER);
tabsec->set_offset(file_offset);
tabsec->set_size(pair->get_e_size());
tabsec->set_file_alignment(1);
tabsec->set_mapped_alignment(1);
tabsec->set_mapped_preferred_rva(pair->get_e_rva().get_rva());
tabsec->set_mapped_actual_va(pair->get_e_rva().get_rva()+get_base_va()); /*FIXME: not sure this is correct. [RPM 2009-09-11]*/
tabsec->set_mapped_size(pair->get_e_size());
tabsec->set_mapped_rperm(true);
tabsec->set_mapped_wperm(false);
tabsec->set_mapped_xperm(false);
pair->set_section(tabsec);
pair->set_e_rva(pair->get_e_rva().set_section(tabsec));
}
/* Now parse the sections */
for (size_t i=0; i<p_rvasize_pairs->get_pairs().size(); i++) {
SgAsmPERVASizePair *pair = p_rvasize_pairs->get_pairs()[i];
SgAsmGenericSection *tabsec = pair->get_section();
if (tabsec)
tabsec->parse();
}
}
示例13: ROSE_ASSERT
void
SgAsmGenericFile::shift_extend(SgAsmGenericSection *s, rose_addr_t sa, rose_addr_t sn, AddressSpace space, Elasticity elasticity)
{
ROSE_ASSERT(s!=NULL);
ROSE_ASSERT(s->get_file()==this);
ROSE_ASSERT((space & (ADDRSP_FILE|ADDRSP_MEMORY)) != 0);
const bool debug = false;
static size_t ncalls=0;
char p[256];
if (debug) {
const char *space_s="unknown";
if (space & ADDRSP_FILE) {
space_s = "file";
} else if (space & ADDRSP_MEMORY) {
space_s = "memory";
}
sprintf(p, "SgAsmGenericFile::shift_extend[%" PRIuPTR "]: ", ncalls++);
fprintf(stderr, "%s -- START --\n", p);
fprintf(stderr, "%s S = [%d] \"%s\"\n", p, s->get_id(), s->get_name()->get_string(true).c_str());
fprintf(stderr, "%s %s Sa=0x%08" PRIx64 " (%" PRIu64 "), Sn=0x%08" PRIx64 " (%" PRIu64 ")\n",
p, space_s, sa, sa, sn, sn);
fprintf(stderr, "%s elasticity = %s\n", p, (ELASTIC_NONE==elasticity ? "none" :
ELASTIC_UNREF==elasticity ? "unref" :
ELASTIC_HOLE==elasticity ? "unref+holes" :
"unknown"));
}
/* No-op case */
if (0==sa && 0==sn) {
if (debug) {
fprintf(stderr, "%s No change necessary.\n", p);
fprintf(stderr, "%s -- END --\n", p);
}
return;
}
bool filespace = (space & ADDRSP_FILE)!=0;
bool memspace = (space & ADDRSP_MEMORY)!=0;
rose_addr_t align=1, aligned_sa, aligned_sasn;
SgAsmGenericSectionPtrList neighbors, villagers;
ExtentMap amap; /* address mappings for all extents */
Extent sp;
/* Get a list of all sections that may need to be adjusted. */
SgAsmGenericSectionPtrList all;
switch (elasticity) {
case ELASTIC_NONE:
case ELASTIC_UNREF:
all = filespace ? get_sections() : get_mapped_sections();
break;
case ELASTIC_HOLE:
all = filespace ? get_sections(false) : get_mapped_sections();
break;
}
if (debug) {
fprintf(stderr, "%s Following sections are in 'all' set:\n", p);
for (size_t i=0; i<all.size(); i++) {
Extent ep;
if (filespace) {
ep = all[i]->get_file_extent();
} else {
ROSE_ASSERT(all[i]->is_mapped());
ep = all[i]->get_mapped_preferred_extent();
}
fprintf(stderr, "%s 0x%08" PRIx64 " 0x%08" PRIx64 " 0x%08" PRIx64 " [%d] \"%s\"\n",
p, ep.relaxed_first(), ep.size(), ep.relaxed_first()+ep.size(), all[i]->get_id(),
all[i]->get_name()->get_string(true).c_str());
}
}
for (size_t pass=0; pass<2; pass++) {
if (debug) {
fprintf(stderr, "%s -- %s --\n",
p, 0==pass?"FIRST PASS":"SECOND PASS (after making a larger hole)");
}
/* S offset and size in file or memory address space */
if (filespace) {
sp = s->get_file_extent();
} else if (!memspace || !s->is_mapped()) {
return; /*nothing to do*/
} else {
sp = s->get_mapped_preferred_extent();
}
/* Build address map */
for (size_t i=0; i<all.size(); i++) {
if (filespace) {
amap.insert(all[i]->get_file_extent());
} else {
ROSE_ASSERT(all[i]->is_mapped());
amap.insert(all[i]->get_mapped_preferred_extent());
}
}
if (debug) {
fprintf(stderr, "%s Address map:\n", p);
amap.dump_extents(stderr, (std::string(p)+" ").c_str(), "amap");
fprintf(stderr, "%s Extent of S:\n", p);
//.........这里部分代码省略.........
示例14: ROSE_ASSERT
//.........这里部分代码省略.........
string name = unparsedInstruction + "\\n" + addressString;
#endif
ROSE_ASSERT(name.empty() == false);
nodelabel += string("\\n") + name;
#else
printf ("Warning: In AstDOTGeneration.C ROSE_BUILD_BINARY_ANALYSIS_SUPPORT is not defined \n");
#endif
}
SgAsmExpression* genericExpression = isSgAsmExpression(node);
if (genericExpression != NULL)
{
#ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
string name = unparseExpression(genericExpression, NULL, NULL);
ROSE_ASSERT(name.empty() == false);
nodelabel += string("\\n") + name;
#else
printf ("Warning: In AstDOTGeneration.C ROSE_BUILD_BINARY_ANALYSIS_SUPPORT is not defined \n");
#endif
}
// DQ (10/29/2008): Added some support for additional output of internal names for specific IR nodes.
// In generall there are long list of these IR nodes in the binary and this helps make some sense of
// the lists (sections, symbols, etc.).
SgAsmExecutableFileFormat* binaryFileFormatNode = isSgAsmExecutableFileFormat(node);
if (binaryFileFormatNode != NULL)
{
#ifdef ROSE_BUILD_BINARY_ANALYSIS_SUPPORT
// The case of binary file format IR nodes can be especially confusing so we want the
// default to output some more specific information for some IR nodes (e.g. sections).
string name;
SgAsmGenericSection* genericSection = isSgAsmGenericSection(node);
if (genericSection != NULL)
{
SgAsmGenericString* genericString = genericSection->get_name();
ROSE_ASSERT(genericString != NULL);
name = genericString->get_string();
}
SgAsmGenericSymbol* genericSymbol = isSgAsmGenericSymbol(node);
if (genericSymbol != NULL)
{
SgAsmGenericString* genericString = genericSymbol->get_name();
ROSE_ASSERT(genericString != NULL);
name = genericString->get_string();
if (name.empty() == true)
name = "no_name_for_symbol";
}
SgAsmGenericDLL* genericDLL = isSgAsmGenericDLL(node);
if (genericDLL != NULL)
{
SgAsmGenericString* genericString = genericDLL->get_name();
ROSE_ASSERT(genericString != NULL);
name = genericString->get_string();
}
SgAsmPEImportItem* peImportItem = isSgAsmPEImportItem(node);
if (peImportItem != NULL)
{
示例15: get_sections_by_va
SgAsmGenericSection *
SgAsmGenericFile::get_best_possible_section_by_va(rose_addr_t va)
{
// This function is implemented for use in:
// "DisassemblerCommon::AsmFileWithData::getSectionOfAddress(uint64_t addr)"
// It supports a more restrictive selection of valid sections to associate with
// a given address so that we can avoid disassembly of sections that are not code.
const std::vector<SgAsmGenericSection*> &possible = get_sections_by_va(va);
if (0 == possible.size())
{
return NULL;
}
else
{
if (1 == possible.size())
{
// printf ("Only one alternative: va = %p possible[0] id = %d name = %s (return %s) \n",
// (void*)va,possible[0]->get_id(),possible[0]->get_name().c_str(),(possible[0]->get_id() < 0) ? "NULL" : "it");
// return possible[0];
if (possible[0]->get_id() < 0)
return NULL;
else
return possible[0];
}
}
#if 0
printf ("Select from %" PRIuPTR " alternatives \n",possible.size());
for (size_t i = 0; i < possible.size(); i++)
{
printf (" va = %p possible[%" PRIuPTR "] id = %d name = %s \n",(void*)va,i,possible[i]->get_id(),possible[i]->get_name().c_str());
}
#endif
/* Choose the "best" section to return. */
SgAsmGenericSection *best = possible[0];
rose_addr_t fo0 = possible[0]->get_va_offset(va);
for (size_t i = 1; i < possible.size(); i++)
{
if (fo0 != possible[i]->get_va_offset(va))
return NULL; /* all possible sections must map the VA to the same file offset */
if (best->get_id() < 0 && possible[i]->get_id() > 0)
{
best = possible[i]; /*prefer sections defined in a section or object table*/
}
else
if (best->get_mapped_size() > possible[i]->get_mapped_size())
{
best = possible[i]; /*prefer sections with a smaller mapped size*/
}
else
if (best->get_name()->get_string().size()==0 && possible[i]->get_name()->get_string().size()>0)
{
best = possible[i]; /*prefer sections having a name*/
}
else
{
/* prefer section defined earlier*/
}
}
ROSE_ASSERT(best != NULL);
// Add a few things that we just don't want to disassemble
if (best->get_name()->get_string() == "ELF Segment Table")
return NULL;
// printf (" best: va = %p id = %d name = %s \n",(void*)va,best->get_id(),best->get_name().c_str());
return best;
}